4

I am trying to convert a string with milliseconds (20180510-10:50:58.106) to date in java. However, when I convert it I get the millisecond part but again in the string. I want the same part in date format.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class SimpleDateFormatExample {
public static void main(String[] args) {

    String curDate = "20180510-10:50:58.106";
    Date SysDateVar = null ;


    //SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd-HH:mm:ss.SSS");

    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd-HH:mm:ss.SSS");
    //String Todate = format.format(curDate);
    //System.out.println("format.parse(curDate)="+Todate);
    String abc = null;

    try
    {

        abc = format.format(format.parse(curDate));

         System.out.println("SysDateVar ="+abc);

    SysDateVar = new Date ((long) (format.parse(curDate)).getTime());
    System.out.println("format.parse(curDate)="+ SysDateVar);
    }
    catch (ParseException e) {

        e.printStackTrace();
    }
}
}

Output Is :

$javac SimpleDateFormatExample.java
$java -Xmx128M -Xms16M SimpleDateFormatExample

SysDateVar =20180510-10:50:58.106

format.parse(curDate)=Thu May 10 10:50:58 UTC 2018

Variable abc is a string. I want the same output in a Date variable.

azro
  • 53,056
  • 7
  • 34
  • 70
chetan
  • 1,385
  • 4
  • 15
  • 31
  • Because the default format (in `Date.toString`) doesn't print the milliseconds. Simply use `format.format(SysDateVar)` to use the formatter you define to get a String or define another one. PS : why do you create `Date` from the `Date.getTime()` retrieve by the `SimpleDateFormatter` ? `SysDateVar = new Date ((long) (format.parse(curDate)).getTime());` -> `SysDateVar = format.parse(curDate);` – AxelH May 11 '18 at 07:27
  • 2
    You seem to be suffering from the misconception that any date/time class contains some kind of formatting information, they don't. They are just simply a representation of some point in time – MadProgrammer May 11 '18 at 07:28
  • Hi Alex, when I try to do this, my milliseconds part is getting truncated. Date SysDateVar = null ; SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd-HH:mm:ss.SSS"); SysDateVar = new Date ((long) (format.parse(curDate)).getTime()); – chetan May 11 '18 at 07:31
  • 1
    no @chetan, it is not truncated but you only check the `date` instance by checking it's default string representation, what you have printed doesn't reflect the `Date` mostly because a `Date` instance doesn't have a format. What you see is the result of [`Date.toString`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#toString--) method to represent the instance in a `String` form (like explained in the answer). – AxelH May 11 '18 at 07:33
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Also the classes of `java.time` will print milliseconds (and smaller) through their`toString` methods if milliseconds are non-zero. – Ole V.V. May 11 '18 at 10:30

1 Answers1

2

When you call System.out.println on a Date object, the toString() method of this Date will be called implictly, and it will return in default format like Thu May 10 10:50:58 UTC 2018.

You need call format.format(date) explictly to get desired output.

xingbin
  • 27,410
  • 9
  • 53
  • 103
  • Hi Liu, Thanx for the reply. Actually, I want to store the output in a date variable. When I try to store it in a date variable by using parse method, milliseconds part is getting truncated. – chetan May 11 '18 at 07:29
  • @chetan you did not unerstand^^ The date contains the millisecond BUT does not print it with `.toString()` because pattern is `EEE MMM dd HH:mm:ss zzz yyyy` http://prntscr.com/jgg3rc – azro May 11 '18 at 07:33