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

204 views Asked by At

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 . what is the possible solution .. I am using node.js at the client side. But I think it has nothing to do with this problem . I WANT TO REMOVE THIS 0

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

There are 3 answers

0
Basil Bourque On

tl;dr

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.

ISO 8601

First replace the SPACE in the middle of your input string to make it comply with the standard ISO 8601 format.

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.

LocalDateTime ldt = LocalDateTime.parse( input );

Generate a String by simply calling toString. By default an ISO 8601 format is used to create the String. 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

If you dislike the T in the middle, replace with a SPACE.

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

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 ldtTruncated = ldt.truncatedTo( ChronoUnit.SECONDS );  // Truncate any fraction of a second.

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

0
mvw On

You are not giving a format pattern string to the SimpleDateFormat constructor, so it uses some default pattern. 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 e.g. in

29.02.2016 23:01:15.999

You want to omit the S format symbols (milliseconds) in the format string. E.g.:

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.

0
K. Gol On

Try to set proper format in SimpleDateFormat constructor. 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);