做爰全过程网站免费的视频教程,餐饮营销策划与运营,有没有关于网站开发的名人访谈,做一个线上app多少钱引言我们在开发过程中#xff0c;在数据库中经常会看到beginTime、updateTime和endTime这些字段#xff0c;这些可能是为了记录业务操作的某个时间、日期等信息。特此#xff0c;总结一些在代码中常用的日期、时间格式化的方法模板。DateFormatDateFormat继承MessageFormat在数据库中经常会看到beginTime、updateTime和endTime这些字段这些可能是为了记录业务操作的某个时间、日期等信息。特此总结一些在代码中常用的日期、时间格式化的方法模板。DateFormat DateFormat继承MessageFormat是实现日期格式化的抽象类。提供两个方法format()用于将数值或日期格化式成字符串parse()方法用于将字符串解析成数值或日期。parse()用法举例String dateStr 2019-12-10;System.out.println(DateFormat.getDateInstance().parse(dateStr);输出结果Thur Dec 10 00:00:00 CST 2019如何得到DateFormat对象getDateInstance()返回一个只有日期无时间的日期格式器getImeInstance()返回一个只有时间没有日期的时间格式器getDateTimeInstance()返回一个既有时间、又有日期的格式器。SimpleDateFormat DateFormat在格式化日期时间方面显得不够灵活需要特定的格式才能解析为了更好的格式化Date它的子类SimpleDateFormat出现了。示例1public class SimpleDateFormatTest{public static void main(String[] args) throws ParseException { Date date new Date();//创建SimpleDateFormat对象 SimpleDateFormat simpleDateFormat1 new SimpleDateFormat(Gyyyy年中第D天);// 将date日期解析为字符串 String dateStr simpleDateFormat1.format(date); System.out.println(公元日期表示: dateStr); String dateStr2 19###十二月##10; SimpleDateFormat simpleDateFormat2 SimpleDateFormat(y###MMM##d);//将字符串解析成日期 System.out.println(一般日期为: simpleDateFormat2.parse(dateStr2)); }}输出结果公元日期表示: 公元2020年中第77天一般日期为: Thur Dec 10 00:00:00 CST 2019示例2Date date new Date();SimpleDateFormat dateFormat new SimpleDateFormat(yyyyMMdd);System.out.println(dateFormat.format(date);运行结果20200317DateTimeFormatter DateTimeFormatter是Java 8 中java.time.format包下新增的格式器类不仅可以把日期或时间对象格式化成字符串也可以把特定格式的字符串解析成日期或时间对象。format()示例public class DateTimeFormatterFormatTest{public static void main(String[] args) { LocalDateTime localDateTime LocalDateTime.now(); DateTimeFormatter dateTimeFormatter DateTimeFormatter.ISO_LOCAL_DATE_TIME; System.out.println(dateTimeFormatter.format(localDateTime )); System.out.println(localDateTime.format(dateTimeFormatter));//全日期时间 DateTimeFormatter dateTimeFormatter2 DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.MEDIUM); System.out.println(dateTimeFormatter2.format(localDateTime )); System.out.println(localDateTime.format(dateTimeFormatter2));//模式字符串创建格式器 DateTimeFormatter dateTimeFormatter3 DateTimeFormatter.ofPattern(Gyyyy-MMM-dd HH:mm:ss); System.out.println(dateTimeFormatter3.format(localDateTime )); System.out.println(localDateTime.format(dateTimeFormatter3)); }}输出结果2020-03-17T22:41:20.2202020-03-17T22:41:20.2202020年3月17日 星期二 22:41:202020年3月17日 星期二 22:41:20公元2020-三月-17 22:41:20公元2020-三月-17 22:41:20parse()示例public class DateTimeFormatterParseTest{public static void main(String[] args) { String str 2020$$$03$$$dd 22时51分10秒 DateTimeFormatter dateTimeFormatter DateTimeFormatter.ofPattern(yyy$$$MMM$$$dd HH时mm分ss秒);//解析日期 LocalDateTime localDateTime LocalDateTime.parse(str, dateTimeFormatter); System.out.println(localDateTime); }}输出结果2020-03-17T22:51:10[每篇微语]明天的期望让我们忘了这天的痛苦。 ——李嘉诚