java时间日期格式化工具类程序

作者:袖梨 2022-06-29

例1.整理了一份可重用的日期格式化工具类,在日常开发中悲催的程序员离不开这个工具类的
下面给大家把java日期工具类代码贡献上:

代码如下 复制代码

/**
* 日期工具类-www.111com.net网整理
* 默认使用 "yyyy-MM-dd HH:mm:ss" 格式化日期
* @author xw素材网
*/
public final class DateUtils {
/**
* 英文简写(默认)如:2010-12-01
*/
public static String FORMAT_SHORT = "yyyy-MM-dd";
/**
* 英文全称 如:2010-12-01 23:15:06
*/
public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
/**
* 精确到毫秒的完整时间 如:yyyy-MM-dd HH:mm:ss.S
*/
public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
/**
* 中文简写 如:2010年12月01日
*/
public static String FORMAT_SHORT_CN = "yyyy年MM月dd";
/**
* 中文全称 如:2010年12月01日 23时15分06秒
*/
public static String FORMAT_LONG_CN = "yyyy年MM月dd日 HH时mm分ss秒";
/**
* 精确到毫秒的完整中文时间
*/
public static String FORMAT_FULL_CN = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒";
/**
* 获得默认的 date pattern
*/
public static String getDatePattern() {
return FORMAT_LONG;
}
/**
* 根据预设格式返回当前日期
* @return
*/
public static String getNow() {
return format(new Date());
}
/**
* 根据用户格式返回当前日期
* @param format
* @return
*/
public static String getNow(String format) {
return format(new Date(), format);
}
/**
* 使用预设格式格式化日期
* @param date
* @return
*/
public static String format(Date date) {
return format(date, getDatePattern());
}
/**
* 使用用户格式格式化日期
* @param date 日期
* @param pattern 日期格式
* @return
*/
public static String format(Date date, String pattern) {
String returnValue = "";
if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
returnValue = df.format(date);
}
return (returnValue);
}
/**
* 使用预设格式提取字符串日期
* @param strDate 日期字符串
* @return
*/
public static Date parse(String strDate) {
return parse(strDate, getDatePattern());
}
/**
* 使用用户格式提取字符串日期
* @param strDate 日期字符串
* @param pattern 日期格式
* @return
*/
public static Date parse(String strDate, String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 在日期上增加数个整月
* @param date 日期
* @param n 要增加的月数
* @return
*/
public static Date addMonth(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, n);
return cal.getTime();
}
/**
* 在日期上增加天数
* @param date 日期
* @param n 要增加的天数
* @return
*/
public static Date addDay(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, n);
return cal.getTime();
}
/**
* 获取时间戳
*/
public static String getTimeString() {
SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
Calendar calendar = Calendar.getInstance();
return df.format(calendar.getTime());
}
/**
* 获取日期年份
* @param date 日期
* @return
*/
public static String getYear(Date date) {
return format(date).substring(0, 4);
}
/**
* 按默认格式的字符串距离今天的天数
* @param date 日期字符串
* @return
*/
public static int countDays (String date) {
long t = Calendar.getInstance().getTime().getTime();
Calendar c = Calendar.getInstance();
c.setTime(parse(date));
long t1 = c.getTime().getTime();
return (int)(t/1000 - t1/1000)/3600/24;
}
/**
* 按用户格式字符串距离今天的天数
* @param date 日期字符串
* @param format 日期格式
* @return
*/
public static int countDays (String date, String format) {
long t = Calendar.getInstance().getTime().getTime();
Calendar c = Calendar.getInstance();
c.setTime(parse(date, format));
long t1 = c.getTime().getTime();
return (int)(t/1000 - t1/1000)/3600/24;
}
}

例2.不但有上面功能,还包含常用的日期格式化方法

代码如下 复制代码

package com.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CommonDateParseUtil {
public static final String ENG_DATE_FROMAT = "EEE, d MMM yyyy HH:mm:ss z";
public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
public static final String YYYY_MM_DD = "yyyy-MM-dd";
public static final String YYYY = "yyyy";
public static final String MM = "MM";
public static final String DD = "dd";

/**
* @param date
* @return
* @作者 王建明
* @创建日期 2012-7-13
* @创建时间 下午12:22:40
* @描述 —— 格式化日期对象
*/
public static Date date2date(Date date, String formatStr) {
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
String str = sdf.format(date);
try {
date = sdf.parse(str);
} catch (Exception e) {
return null;
}
return date;
}

/**
* @param date
* @return
* @作者 王建明
* @创建日期 2012-7-13
* @创建时间 下午12:24:19
* @描述 —— 时间对象转换成字符串
*/
public static String date2string(Date date, String formatStr) {
String strDate = "";
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
strDate = sdf.format(date);
return strDate;
}

/**
* @param date
* @return
* @作者 王建明
* @创建日期 2012-7-13
* @创建时间 下午12:24:19
* @描述 —— sql时间对象转换成字符串
*/
public static String timestamp2string(Timestamp timestamp, String formatStr) {
String strDate = "";
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
strDate = sdf.format(timestamp);
return strDate;
}

/**
* @param dateString
* @param formatStr
* @return
* @作者 王建明
* @创建日期 2012-7-13
* @创建时间 下午1:09:24
* @描述 —— 字符串转换成时间对象
*/
public static Date string2date(String dateString, String formatStr) {
Date formateDate = null;
DateFormat format = new SimpleDateFormat(formatStr);
try {
formateDate = format.parse(dateString);
} catch (ParseException e) {
return null;
}
return formateDate;
}

/**
* @param date
* @return
* @作者 王建明
* @创建日期 2012-10-10
* @创建时间 上午09:18:36
* @描述 —— Date类型转换为Timestamp类型
*/
public static Timestamp date2timestamp(Date date) {
if (date == null)
return null;
return new Timestamp(date.getTime());
}

/**
* @return
* @作者 王建明
* @创建日期 2012-9-13
* @创建时间 下午05:02:57
* @描述 —— 获得当前年份
*/
public static String getNowYear() {
SimpleDateFormat sdf = new SimpleDateFormat(YYYY);
return sdf.format(new Date());
}

/**
* @return
* @作者 王建明
* @创建日期 2012-9-13
* @创建时间 下午05:03:15
* @描述 —— 获得当前月份
*/
public static String getNowMonth() {
SimpleDateFormat sdf = new SimpleDateFormat(MM);
return sdf.format(new Date());
}

/**
* @return
* @作者 王建明
* @创建日期 2013-01-24
* @创建时间 08:41:47
* @描述 —— 获得当前日期中的日
*/
public static String getNowDay(){
SimpleDateFormat sdf = new SimpleDateFormat(DD);
return sdf.format(new Date());
}

/**
* @param time
* @return
* @作者 王建明
* @创建日期 2012-6-17
* @创建时间 上午10:19:31
* @描述 —— 指定时间距离当前时间的中文信息
*/
public static String getLnow(long time) {
Calendar cal = Calendar.getInstance();
long timel = cal.getTimeInMillis() - time;
if (timel / 1000 return "1分钟以内";
} else if (timel / 1000 / 60 return timel / 1000 / 60 + "分钟前";
} else if (timel / 1000 / 60 / 60 return timel / 1000 / 60 / 60 + "小时前";
} else {
return timel / 1000 / 60 / 60 / 24 + "天前";
}
}
}

相关文章

精彩推荐