修改date格式 java_如何用Java更改日期格式?

LocalDate.parse(

"23/01/2017" ,

DateTimeFormatter.ofPattern( "dd/MM/uuuu" , Locale.UK ) ).format(

DateTimeFormatter.ofPattern( "uuuu/MM/dd" , Locale.UK ))2017/01/23

避免遗留日期-时间类

这个克里斯托弗·帕克的答复是正确但过时的。麻烦的旧日期时间类,如java.util.Date, java.util.Calendar,和java.text.SimpleTextFormat现在遗产,被java.time上课。

使用java.time

将输入字符串解析为日期时间对象,然后以所需格式生成新的字符串对象。

这个LocalDate类表示一个日期纯值,没有一天的时间,也没有时区.DateTimeFormatter fIn = DateTimeFormatter.ofPattern( "dd/MM/uuuu" , Locale.UK );  // As a habit, specify the desired/expected locale, though in this case the locale is irrelevant.LocalDate ld = LocalDate.parse( "23/01/2017" , fIn );

为输出定义另一个格式化程序。DateTimeFormatter fOut = DateTimeFormatter.ofPattern( "uuuu/MM/dd" , Locale.UK );String output = ld.format( fOut );2017/01/23

顺便说一下,考虑使用标准ISO 8601表示日期时间值的字符串的格式。

关于java.time

若要了解更多信息,请参见Oracle教程..并搜索堆栈溢出的许多例子和解释。规格是JSR 310.

在哪里获得java.time类?内置的。

带有捆绑实现的标准JavaAPI的一部分。

Java 9添加了一些次要的特性和修复。中的大部分java.time功能都被移植到Java 6&7中。这个项目适应

Threeten-Backport(上面提到)专门用于Android。

这个三次-额外项目使用其他类扩展java.time。这个项目是java.time将来可能添加的一个试验场。您可能会在这里发现一些有用的类,例如Interval, YearWeek, YearQuarter,和更多.

尤达-时间

最新情况:这个尤达-时间项目现在在维护模式,小组建议迁移到java.time上课。这里的这一节是为了历史而留下的。

为了好玩,下面是他的代码,用于使用尤达-时间图书馆。// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.// import org.joda.time.*;// import org.joda.time.format.*;final String OLD_FORMAT = "dd/MM/yyyy";final String NEW_FORMAT = "yyyy/MM/dd";// August 12, 2010String oldDateString = "12/08/2010";String newDateString;DateTimeFormatter formatterOld = DateTimeFormat.forPattern(OLD_FORMAT);DateTimeFormatter formatterNew = DateTimeFormat.forPattern(NEW_FORMAT);LocalDate localDate = formatterOld.parseLocalDate( oldDateString );newDateString = formatterNew.print( localDate );

转储到控制台…System.out.println( "localDate: " + localDate );System.out.println( "newDateString: " + newDateString );

运行…时localDate: 2010-08-12

newDateString: 2010/08/12

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值