简体   繁体   English

java.util.Date 返回 --> 2016-12-26 14:18:57.0 想要删除这个 .0

[英]java.util.Date returning --> 2016-12-26 14:18:57.0 want to remove this .0

this is the code which returns Date but at the client side i am getting这是返回日期的代码,但在客户端我得到

2016-12-26 14:18:57.0 at the client side . 2016-12-26 14:18:57.0 在客户端。 what is the possible solution .. I am using node.js at the client side.可能的解决方案是什么.. 我在客户端使用 node.js。 But I think it has nothing to do with this problem .但我认为这与这个问题无关。 I WANT TO REMOVE THIS 0我想删除这个 0

SimpleDateFormat sdf = new SimpleDateFormat(format);
    Date date = sdf.parse(value);
    if (!value.equals(sdf.format(date))) {
      date = null;
    }
    return date != null;

You are not giving a format pattern string to the SimpleDateFormat constructor, so it uses some default pattern .您没有向 SimpleDateFormat 构造函数提供格式模式字符串,因此它使用一些默认模式 The solution is to provide a proper format string.解决方案是提供正确的格式字符串。

The formatting can be done like this:格式化可以这样完成:

SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss.SSS")

which would result eg in这将导致例如

29.02.2016 23:01:15.999

You want to omit the S format symbols (milliseconds) in the format string.您想在格式字符串中省略 S 格式符号(毫秒)。 Eg:例如:

SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss")

Of course you need to change this example to your format needs.当然,您需要将此示例更改为您的格式需要。 Play a bit with the order and number of symbols, confer the docs .稍微玩一下符号的顺序和数量,授予docs

Try to set proper format in SimpleDateFormat constructor.尝试在 SimpleDateFormat 构造函数中设置正确的格式。 Something like this:像这样的东西:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String value = "2016-12-26 14:18:57";
Date date = sdf.parse(value);
if (!value.equals(sdf.format(date))) {
    System.out.println("not equals");
}
System.out.println("date = " + date);

tl;dr tl;博士

LocalDateTime.parse( 
    "2016-12-26 14:18:57.0".replace( " " , "T" )
)
.truncatedTo( ChronoUnit.SECONDS )
.toString()
.replace( "T" , " " )

Avoid legacy date-time classes避免遗留的日期时间类

You are using troublesome old date-time classes, now legacy, supplanted by the java.time classes.您正在使用麻烦的旧日期时间类,现在是遗留的,被 java.time 类取代。

ISO 8601 ISO 8601

First replace the SPACE in the middle of your input string to make it comply with the standard ISO 8601 format.首先替换输入字符串中间的空格,使其符合标准 ISO 8601 格式。

String input = "2016-12-26 14:18:57.0".replace( " " , "T" );

LocalDateTime

Parse as a LocalDateTime given that your input string lacks any indication of offset-from-UTC or time zone.考虑到您的输入字符串缺少任何从 UTC 或时区偏移的指示,解析为LocalDateTime

LocalDateTime ldt = LocalDateTime.parse( input );

Generate a String by simply calling toString .通过简单地调用toString生成一个字符串。 By default an ISO 8601 format is used to create the String.默认情况下,使用 ISO 8601 格式创建字符串。 If the value has no fractional second, no decimal mark nor any fractional seconds digits appear.如果该值没有小数秒,则不会出现小数点或任何小数秒数字。

String output = ldt.toString ();

ldt.toString(): 2016-12-26T14:18:57 ldt.toString(): 2016-12-26T14:18:57

If you dislike the T in the middle, replace with a SPACE.如果您不喜欢中间的T ,请替换为 SPACE。

output = output.replace( "T" , " " );

2016-12-26 14:18:57 2016-12-26 14:18:57

If your input might carry a fractional second and you want to delete that portion of data entirely rather than merely suppress its display, truncate the LocalDateTime object.如果您的输入可能带有小数秒,并且您想要完全删除该部分数据而不是仅仅抑制其显示,请截断LocalDateTime对象。

LocalDateTime ldtTruncated = ldt.truncatedTo( ChronoUnit.SECONDS );  // Truncate any fraction of a second.

About java.time关于 java.time

The java.time framework is built into Java 8 and later. java.time框架内置于 Java 8 及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat .这些类取代麻烦的老传统日期时间类,如java.util.DateCalendar ,和SimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes.现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial .要了解更多信息,请参阅Oracle 教程 And search Stack Overflow for many examples and explanations.并在 Stack Overflow 上搜索许多示例和解释。 Specification is JSR 310 .规范是JSR 310

Where to obtain the java.time classes?从哪里获得 java.time 类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目用额外的类扩展了 java.time。 This project is a proving ground for possible future additions to java.time.该项目是未来可能添加到 java.time 的试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more .您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

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

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