1.一般(默认格式):
格式:星期 月份 日期 时:分:秒 时区 年份
Thu Aug 11 15:19:59 CST 2022
2.simpleDateFormat格式化date类:
import java.text.SimpleDateFormat;
import java.util.Date;
public class date {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(date);
System.out.println(time);
}
}
格式:年份-月份-日期 时:分:秒
2022-08-11 15:25:00
simpleDateFormat的格式化正则:
3.DateFormat类对date数据的格式进行转换:
源码:
/**
* Useful constant for ERA field alignment.
* Used in FieldPosition of date/time formatting.
*/
public final static int ERA_FIELD = 0;
/**
* Useful constant for YEAR field alignment.
* Used in FieldPosition of date/time formatting.
*/
public final static int YEAR_FIELD = 1;
/**
* Useful constant for MONTH field alignment.
* Used in FieldPosition of date/time formatting.
*/
public final static int MONTH_FIELD = 2;
/**
* Useful constant for DATE field alignment.
* Used in FieldPosition of date/time formatting.
*/
public final static int DATE_FIELD = 3;
/**
* Useful constant for one-based HOUR_OF_DAY field alignment.
* Used in FieldPosition of date/time formatting.
* HOUR_OF_DAY1_FIELD is used for the one-based 24-hour clock.
* For example, 23:59 + 01:00 results in 24:59.
*/
public final static int HOUR_OF_DAY1_FIELD = 4;
/**
* Useful constant for zero-based HOUR_OF_DAY field alignment.
* Used in FieldPosition of date/time formatting.
* HOUR_OF_DAY0_FIELD is used for the zero-based 24-hour clock.
* For example, 23:59 + 01:00 results in 00:59.
*/
public final static int HOUR_OF_DAY0_FIELD = 5;
/**
* Useful constant for MINUTE field alignment.
* Used in FieldPosition of date/time formatting.
*/
public final static int MINUTE_FIELD = 6;
/**
* Useful constant for SECOND field alignment.
* Used in FieldPosition of date/time formatting.
*/
public final static int SECOND_FIELD = 7;
/**
* Useful constant for MILLISECOND field alignment.
* Used in FieldPosition of date/time formatting.
*/
public final static int MILLISECOND_FIELD = 8;
/**
* Useful constant for DAY_OF_WEEK field alignment.
* Used in FieldPosition of date/time formatting.
*/
public final static int DAY_OF_WEEK_FIELD = 9;
/**
* Useful constant for DAY_OF_YEAR field alignment.
* Used in FieldPosition of date/time formatting.
*/
public final static int DAY_OF_YEAR_FIELD = 10;
/**
* Useful constant for DAY_OF_WEEK_IN_MONTH field alignment.
* Used in FieldPosition of date/time formatting.
*/
public final static int DAY_OF_WEEK_IN_MONTH_FIELD = 11;
/**
* Useful constant for WEEK_OF_YEAR field alignment.
* Used in FieldPosition of date/time formatting.
*/
public final static int WEEK_OF_YEAR_FIELD = 12;
/**
* Useful constant for WEEK_OF_MONTH field alignment.
* Used in FieldPosition of date/time formatting.
*/
public final static int WEEK_OF_MONTH_FIELD = 13;
/**
* Useful constant for AM_PM field alignment.
* Used in FieldPosition of date/time formatting.
*/
public final static int AM_PM_FIELD = 14;
/**
* Useful constant for one-based HOUR field alignment.
* Used in FieldPosition of date/time formatting.
* HOUR1_FIELD is used for the one-based 12-hour clock.
* For example, 11:30 PM + 1 hour results in 12:30 AM.
*/
public final static int HOUR1_FIELD = 15;
/**
* Useful constant for zero-based HOUR field alignment.
* Used in FieldPosition of date/time formatting.
* HOUR0_FIELD is used for the zero-based 12-hour clock.
* For example, 11:30 PM + 1 hour results in 00:30 AM.
*/
public final static int HOUR0_FIELD = 16;
/**
* Useful constant for TIMEZONE field alignment.
* Used in FieldPosition of date/time formatting.
*/
public final static int TIMEZONE_FIELD = 17;
含义:
/**
*ERA场对齐的有用常数。
*用于日期/时间格式的字段位置。
*/
public final static int ERA_FIELD = 0;
/**
*年字段对齐的有用常量。
*用于日期/时间格式的字段位置。
*/
public final static int YEAR_FIELD = 1;
/**
*用于月字段对齐的有用常量。
*用于日期/时间格式的字段位置。
*/
public final static int MONTH_FIELD = 2;
/**
*用于日期字段对齐的有用常量。
*用于日期/时间格式的字段位置。
*/
public final static int DATE_FIELD = 3;
/**
*一天中一小时字段对齐的有用常数。
*用于日期/时间格式的字段位置。
*DAY1_字段用于基于一的24小时时钟。
*例如,23:59+01:00导致24:59。
*/
public final static int HOUR_OF_DAY1_FIELD = 4;
/**
*用于基于零的HOUR_OF_DAY字段对齐的有用常数。
*用于日期/时间格式的字段位置。
*DAY0_字段用于基于零的24小时时钟。
*例如,23:59+01:00导致00:59。
*/
public final static int HOUR_OF_DAY0_FIELD = 5;
/**
*用于微小场对齐的有用常数。
*用于日期/时间格式的字段位置。
*/
public final static int MINUTE_FIELD = 6;
/**
*用于第二场对齐的有用常数。
*用于日期/时间格式的字段位置。
*/
public final static int SECOND_FIELD = 7;
/**
*毫秒场对齐的有用常数。
*用于日期/时间格式的字段位置。
*/
public final static int MILLISECOND_FIELD = 8;
/**
*用于DAY_OF_WEEK字段对齐的有用常数。
*用于日期/时间格式的字段位置。
*/
public final static int DAY_OF_WEEK_FIELD = 9;
/**
*用于DAY_ OF_YEAR字段对齐的有用常数。
*用于日期/时间格式的字段位置。
*/
public final static int DAY_OF_YEAR_FIELD = 10;
/**
*周中月字段对齐的有用常数。
*用于日期/时间格式的字段位置。
public final static int DAY_OF_WEEK_IN_MONTH_FIELD = 11;
/**
*对于WEEK_ OF_YEAR字段对齐,这是一个有用的常数。
*用于日期/时间格式的字段位置。
*/
public final static int WEEK_OF_YEAR_FIELD = 12;
/**
*用于WEEK_OF_MONTH字段对齐的有用常数。
*用于日期/时间格式的字段位置。
*/
public final static int WEEK_OF_MONTH_FIELD = 13;
/**
*用于AM_ PM场对齐的有用常数。
*用于日期/时间格式的字段位置。
*/
public final static int AM_PM_FIELD = 14;
/**
*一小时字段对齐的有用常数。
*用于日期/时间格式的字段位置。
*HOUR1_ FIELD用于基于一的12小时时钟。
*例如,晚上11:30+1小时导致上午12:30。
*/
public final static int HOUR1_FIELD = 15;
/**
*用于基于零的小时字段对齐的有用常数。
*用于日期/时间格式的字段位置。
*HOUR0_ FIELD用于基于零的12小时时钟。
*例如,晚上11:30+1小时导致上午00:30。
*/
public final static int HOUR0_FIELD = 16;
/**
*时区字段对齐的有用常数。
*用于日期/时间格式的字段位置。
*/
public final static int TIMEZONE_FIELD = 17;
例:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class date {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
//SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(date);
System.out.println(time);
//DateFormat
DateFormat di = DateFormat.getDateInstance();
String time2 = di.format(date);
System.out.println(time2);
//DateFormat2
DateFormat di2 = DateFormat.getDateInstance(DateFormat.FULL);
String time3 = di2.format(date);
System.out.println(time3);
}
}
参考资料:https://jingyan.baidu.com/article/86112f13d566c52736978759.html