繁体   English   中英

格式化字符串到日期的转换而无需更改Java中的格式

[英]formatted string to date conversion without changing format in java

我已经以字符串形式格式化了日期,并且我希望它以日期格式而不更改格式化模式

这是我的代码

Date currDate = new Date();//Fri Oct 31 03:48:24 PDT 2014

    String pattern = "yyyy-MM-dd hh:mm:ss";
    SimpleDateFormat formatter;
    formatter = new SimpleDateFormat(pattern);
    String formattedDate= formatter.format(currDate);//2014-10-31 04:23:42

这里的格式是“ yyyy-MM-dd hh:mm:ss”,而我想要的格式与日期相同。

 SimpleDateFormat sdf = new SimpleDateFormat(pattern);
 Date paidDate = sdf.parse(formattedDate);

System.out.println(pattern + " " + paidDate);//Fri Oct 31 03:48:24 PDT 2014

但我得到的结果是2014年10月31日星期五(星期五)03:48:24,所以请帮助我以日期格式得到2014-10-31 04:23:42的结果

如果我正确理解您的问题:

System.out.println(pattern + " " + sdf.format(paidDate);

您似乎误以为Date对象以某种方式编码了原始日期的格式。 没有。

所以...当您这样做时:

Date paidDate = sdf.parse(formattedDate);

它不会“记住” paidDate日期文本形式的原始格式。 而且它不能。 如果要以默认格式以外的任何格式打印/取消解析日期,则应使用DateFormat并调用其format方法。 调用toString()只会为您提供默认格式的日期。

尝试这个。

System.out.println(formatter.format(paidDate));

tl; dr

不要将日期时间对象与表示其值的字符串混淆。 日期时间对象没有“格式”。

ZonedDateTime.now(                                         // Instantiate a `ZonedDateTime` object capturing the current moment.
    ZoneId.of( "Africa/Tunis" )                            // Assign this time zone through which we see the wall-clock time used by the people of this particular region.
).format(                                                  // Generate a string representing the value of this `ZonedDateTime` object.
    DateTimeFormatter.ofPattern( "uuuu-MM-dd HH:mm:ss" )   // Define a formatting pattern to match your desire.
)

2018-03-10 07:36:23

调用ZonedDateTime::toString生成标准ISO 8601格式的字符串。

2018-03-10T07:36:23.595362 + 01:00 [非洲/突尼斯]

日期时间对象没有“格式”

您正在将Java中的日期时间对象或存储在数据库中的日期时间值与文本表示混淆。 您可以从日期时间对象(或数据库值)生成字符串,但是该字符串是独立的,并且与它表示的值不同。 不要将字符串与其生成者混为一谈。

java.time

避免使用麻烦的旧日期时间类,例如DateCalendarSimpleDateFormat 而是分别使用InstantZonedDateTimeDateTimeFormatter类。

如果你有一个输入的字符串,如2014-10-31 04:23:42 ,以取代在中间SPACE T符合ISO 8601标准格式。 解析/生成字符串时, java.time类默认使用ISO 8601格式。

String input = "2014-10-31 04:23:42".replace( " " , "T" ) ;

该输入缺少任何时区或UTC偏移量指示符。 因此,将其解析为LocalDateTime ,故意缺少区域/偏移量的任何概念。

LocalDateTime ldt = LocalDateTime.parse( input ) ;

ldt.toString():2014-10-31T04:23:42

一个LocalDateTime 不代表时刻,是不是在时间轴上的一个点。 要确定实际力矩,必须提供区域或偏移量的上下文。

ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;  // Now we have an actual moment, a point on the timeline.

要使用UTC捕获当前时刻,请使用Instant

Instant instant = Instant.now() ;

调整到另一个区域。

ZonedDateTime zdt = instant.atZone( z ) ;

zdt.toString():2018-03-10T07:36:23.595362 + 01:00 [非洲/突尼斯]

我不建议生成缺少区域/偏移量指示符的字符串。 但是,如果您坚持使用,请使用内置的DateTimeFormatter ,然后将中间的T替换为SPACE,以获取所需的格式。

String output = zdt.format( DateTimeFormatter.ISO_LOCAL_DATE_TIME ).replace( "T" , " " ) ;

2018-03-10 07:36:23.595362

如果您确实不希望小数,请定义自己的格式设置模式。

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd HH:mm:ss" ) ; 
String output = zdt.format( f ) ;

2018-03-10 07:36:23


关于java.time

java.time框架内置于Java 8及更高版本中。 这些类取代了麻烦的旧的旧式日期时间类,例如java.util.DateCalendarSimpleDateFormat

现在处于维护模式Joda-Time项目建议迁移到java.time类。

要了解更多信息,请参见Oracle教程 并在Stack Overflow中搜索许多示例和说明。 规格为JSR 310

您可以直接与数据库交换java.time对象。 使用与JDBC 4.2或更高版本兼容的JDBC驱动程序 不需要字符串,不需要java.sql.*类。

在哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展了java.time。 该项目为将来可能在java.time中添加内容提供了一个试验场。 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM