格式化字符串到日期转换而不更改java中的格式

时间:2014-10-31 10:55:54

标签: 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的日期格式获得结果

4 个答案:

答案 0 :(得分:3)

如果我理解你的问题:

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

答案 1 :(得分:2)

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

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

Date paidDate = sdf.parse(formattedDate);

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

答案 2 :(得分:0)

试试这个。

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

答案 3 :(得分:0)

TL;博士

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

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,请将中间的空格替换为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以获得所需的格式。

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及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendar和& SimpleDateFormat

现在位于Joda-Timemaintenance mode项目建议迁移到java.time类。

要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310

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

从哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在此处找到一些有用的课程,例如IntervalYearWeekYearQuartermore