0 背景
我们有时要获取时间,年月日时分秒周几,有时要以特定的格式出现。这时就要借助 SimpleDateFormat 或者 DateTimeFormatter。有时要某个月份有多少天需要借助 Calendar。所以有必要了解一些知识。
1 SimpleDateFormat
simpledateFormat 线程不安全,DateTimeFormatter 线程安全。
// 用法
String string = new SimpleDateFormat(String PATTERN, Locale locale).format(Date date);
PATTERN 的样式有很多。具体可以看源码。
public static final String STAND_TIME = "yyyy-MM-dd HH:mm:ss";
public static final String FULL_TIME = "yyyy-MM-dd HH:mm:ss.SSS";
public static final String YEAR_MONTH_DAY = "yyyy-MM-dd";
public static final String YEAR_MONTH_DAY_CN = "yyyy年MM月dd日";
public static final String HOUR_MINUTE_SECOND = "HH:mm:ss";
public static final String HOUR_MINUTE_SECOND_CN = "HH时mm分ss秒";
public static final String YEAR = "yyyy";
public static final String MONTH = "MM";
public static final String DAY = "dd";
public static final String WEEK = "E";
public static final String HOUR = "HH";
public static final String MINUTE = "mm";
public static final String SECOND = "ss";
public static final String MILLISECOND = "SSS";
2 Calendar
Calendar calendar = Calendar.getInstance();
// 设置一个日历
calendar.set(Calendar.DATE,int);
calendar.set(Calendar.MONTH,int);
calendar.set(Calendar.YEAR,int);
// 年月日的增加,可正可负
calendar.add(Calendar.DATE,int);
calendar.add(Calendar.MONTH,int);
calendar.add(Calendar.YEAR,int);
// 年月日的回滚,不会影响大字段。如增加日,不会影响月,31 -> 1,不改变月份。
calendar.roll(Calendar.DATE,int);
calendar.roll(Calendar.MONTH,int);
calendar.roll(Calendar.YEAR,int);
另外注意的参数
// week 是从星期天开始算的 1 - > 7
int a = calandar.get(Calendar.DAY_OF_WEEK);
// month 是从 0 开始的, 0 -> 11
int month = calendar.get(Calendar.MONTH);
3 时间工具 DateUtil
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Objects;public class DateUtil {public static final String STAND_TIME = "yyyy-MM-dd HH:mm:ss";public static final String FULL_TIME = "yyyy-MM-dd HH:mm:ss.SSS";public static final String YEAR_MONTH_DAY = "yyyy-MM-dd";public static final String YEAR_MONTH_DAY_CN = "yyyy年MM月dd日";public static final String HOUR_MINUTE_SECOND = "HH:mm:ss";public static final String HOUR_MINUTE_SECOND_CN = "HH时mm分ss秒";public static final String YEAR = "yyyy";public static final String MONTH = "MM";public static final String DAY = "dd";public static final String WEEK = "E";public static final String HOUR = "HH";public static final String MINUTE = "mm";public static final String SECOND = "ss";public static final String MILLISECOND = "SSS";public static final String YESTERDAY = "昨天";public static final String TODAY = "今天";public static final String TOMORROW = "明天";/*** 获得当前时间** @return 例如 2023-09-29 10:00:00*/public static String getCurrentDateTime() {return new SimpleDateFormat(STAND_TIME, Locale.CHINESE).format(new Date());}/*** 获得当前完整时间** @return 例如 2023-09-29 10:00:00.123*/public static String getCurrentFullDateTime() {return new SimpleDateFormat(FULL_TIME, Locale.CHINESE).format(new Date());}/*** 获得今天年月日** @return 例如 2023-09-29*/public static String getCurrentYearMonthDay() {return new SimpleDateFormat(YEAR_MONTH_DAY, Locale.CHINESE).format(new Date());}/*** 获得年月日 中文版** @return 例如 2023年9月29日*/public static String getCurrentYearMonthDayCn() {return new SimpleDateFormat(YEAR_MONTH_DAY_CN, Locale.CHINESE).format(new Date());}/*** 获得年月日 自定义分隔符** @param delimiter 分隔符* @return 例如 2023/9/29*/public static String getCurrentYearMonthDayDelimiter(CharSequence delimiter) {return new SimpleDateFormat(YEAR + delimiter + MONTH + delimiter + DAY, Locale.CHINESE).format(new Date());}/*** 获得时分秒** @return 例如 10:00:00*/public static String getCurrentHourMinuteSecond() {return new SimpleDateFormat(HOUR_MINUTE_SECOND, Locale.CHINESE).format(new Date());}/*** 获得时分秒 中文版** @return 例如 10时00分00秒*/public static String getCurrentHourMinuteSecondCn() {return new SimpleDateFormat(HOUR_MINUTE_SECOND_CN, Locale.CHINESE).format(new Date());}/*** 获取时分秒 分隔符** @param delimiter 分隔符* @return 例如 2021/07/01*/public static String getCurrentHourMinuteSecondDelimiter(CharSequence delimiter) {return new SimpleDateFormat(HOUR + delimiter + MINUTE + delimiter + SECOND, Locale.CHINESE).format(new Date());}/*** 获得年** @return 例如 2023*/public static String getCurrentYear() {return new SimpleDateFormat(YEAR, Locale.CHINESE).format(new Date());}/*** 获得月** @return 例如 09*/public static String getCurrentMonth() {return new SimpleDateFormat(MONTH, Locale.CHINESE).format(new Date());}/*** 获得日** @return 29*/public static String getCurrentDay() {return new SimpleDateFormat(DAY, Locale.CHINESE).format(new Date());}/*** 获得时** @return 例如 10*/public static String getCurrentHour() {return new SimpleDateFormat(HOUR, Locale.CHINESE).format(new Date());}/*** 获得时分秒** @return 例如 00*/public static String getCurrentMinute() {return new SimpleDateFormat(MINUTE, Locale.CHINESE).format(new Date());}/*** 获得秒** @return 例如 00*/public static String getCurrentSecond() {return new SimpleDateFormat(SECOND, Locale.CHINESE).format(new Date());}/*** 获得毫秒** @return 例如 123*/public static String getCurrentMillisecond() {return new SimpleDateFormat(MILLISECOND, Locale.CHINESE).format(new Date());}/*** 获得当前时间戳** @return 例如 2023-9-29 10:00:00 为1695952800*/public static long getCurrentTimestamp() {return System.currentTimeMillis();}/*** 将时间转换成时间戳** @param time 时间* @return 返回时间戳 long*/public static long dateToStamp(String time) {SimpleDateFormat simpleDateFormat = new SimpleDateFormat(STAND_TIME, Locale.CHINESE);Date date = null;try {date = simpleDateFormat.parse(time);} catch (Exception e) {e.printStackTrace();}return Objects.requireNonNull(date).getTime();}/*** 将时间戳转换成时间** @param stamp 时间戳* @return 例如 2023-9-29 10:00:00*/public static String stampToDate(long stamp) {return new SimpleDateFormat(STAND_TIME, Locale.CHINESE).format(stamp);}/*** 返回今天是星期几** @return 例如 周五*/public static String getCurrentWeek() {return new SimpleDateFormat(WEEK, Locale.CHINESE).format(new Date());}/*** @param dateTime 日期 例如 2023-09-29* @return 例如 周五*/public static String getWeekOf(String dateTime) {Date date;if ("".equals(dateTime)) {date = new Date();} else {SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY, Locale.CHINESE);try {date = sdf.parse(dateTime);} catch (Exception e) {date = new Date();e.printStackTrace();}}return new SimpleDateFormat(WEEK,Locale.CHINESE).format(date);}/*** @param dateTime 日期 例如 2023-09-29* @return 例如 2023-09-28*/public static String getYesterdayOf(String dateTime) {SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY, Locale.CHINESE);Date date;try {date = sdf.parse(dateTime);} catch (ParseException e) {date = null;e.printStackTrace();}Calendar calendar = new GregorianCalendar();if (date != null) {calendar.setTime(date);}calendar.add(Calendar.DATE, -1);date = calendar.getTime();return new SimpleDateFormat(YEAR_MONTH_DAY, Locale.CHINESE).format(date);}/*** 获取输入日期的明天** @param dateTime 例如 2023-09-29* @return 例如 2023-09-30*/public static String getTomorrowOf(String dateTime) {SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY, Locale.CHINESE);Date date;try {date = sdf.parse(dateTime);} catch (ParseException e) {date = null;e.printStackTrace();}Calendar calendar = new GregorianCalendar();if (date != null) {calendar.setTime(date);}calendar.add(Calendar.DATE, +1);date = calendar.getTime();return new SimpleDateFormat(YEAR_MONTH_DAY, Locale.CHINESE).format(date);}/*** @param dateTime 一个时间 例如 2023-9-29* @return 相对今天而言是什么,比如今天是2023-9-30,返回昨天。*/public static String getDayInfoOf(String dateTime) {String dayInfo;String toDay = getCurrentYearMonthDay();String yesterday = getYesterdayOf(toDay);String tomorrow = getTomorrowOf(toDay);if (dateTime.equals(yesterday)) {dayInfo = YESTERDAY;} else if (dateTime.equals(toDay)) {dayInfo = TODAY;} else if (dateTime.equals(tomorrow)) {dayInfo = TOMORROW;} else {dayInfo = getWeekOf(dateTime);}return dayInfo;}/*** 返回当前月份的天数** @return 例如 9月份 返回30*/public static int getCurrentDaysOfMonth() {Calendar calendar = new GregorianCalendar();//把日期设置为当月第一天calendar.set(Calendar.DATE, 1);//日期回滚一天,也就是最后一天,roll 方法不更改大字段。calendar.roll(Calendar.DATE, -1);return calendar.get(Calendar.DATE);}/*** 返回指定年份月份的天数** @param year 年份 例如 2023* @param month 月份 例如 09* @return 例如 30*/public static int getDaysOfMothOf(int year, int month) {Calendar calendar = new GregorianCalendar();calendar.set(Calendar.YEAR, year);calendar.set(Calendar.MONTH, month - 1);//把日期设置为当月第一天calendar.set(Calendar.DATE, 1);//日期回滚一天,也就是最后一天,roll 方法不更改大字段。calendar.roll(Calendar.DATE, -1);return calendar.get(Calendar.DATE);}}