I want to convert system date and time in 2016-02-14T15:50:39Z format. How to achieve using java?
-
1You ever heared about SimpleDateFormat? – Jens Feb 17 '16 at 09:56
-
Please please please READ -> https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html !! – Vinay Veluri Feb 17 '16 at 09:57
-
Ya I heard but I am not able to convert so I asked. Can anyone help me out?? – Sourabh Feb 17 '16 at 10:04
-
Similar to [this other Question](http://stackoverflow.com/q/3914404/642706), but that one truncates to whole minute whereas this Question truncates to whole second. – Basil Bourque Feb 17 '16 at 21:32
3 Answers
tl;dr
Instant.now()
.toString()
2018-01-23T01:23:45.678901Z
Details
The other answers use old outmoded classes.
ISO 8601
Your desired format complies with the ISO 8601 standard.
The Z
on the end stands for Zulu
which means UTC.
java.time
In Java 8 and later, use the built-in java.time framework.
The java.time classes use ISO 8601 formats by default when parsing/generating textual representations of date-time values.
Instant
An Instant
is a moment on the time line in UTC. Its now
method gets the current moment. As of Java 8 Update 74 that now
method gets the current moment with millisecond resolution but future versions may get up to the full nanosecond resolution which can fit in an Instant
.
The Instant::toString
method generates a String just as you desire, using groups of digits (0, 3, 6, or 9) as needed for the fractional second.
String output = Instant.now().toString(); // Example: 2016-02-14T15:50:39.123Z
Truncate fractional second
If do not care about the fraction of a second, as seen in your Question’s example, truncate with a call to with
, passing the ChronoField.NANO_OF_SECOND
enum.
String output = Instant.now().with( ChronoField.NANO_OF_SECOND , 0 ).toString(); // Example: 2016-02-14T15:50:39Z (no '.123' at end)
Even simpler, call truncatedTo
method, passing a ChronoUnit.SECONDS
enum.
String output = Instant.now().truncatedTo( ChronoUnit.SECONDS ).toString();
If you want to truncate to whole minute rather than whole second, see this other Question.

- 303,325
- 100
- 852
- 1,154
Try this code:
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args)
{
Date dt=new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String formattedDate = formatter.format(dt);
System.out.println(formattedDate);
}
}
You can try several others date formats here.

- 1,607
- 2
- 16
- 25
use simple date format below is example :
Date curDate = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
String DateToStr = format.format(curDate);
System.out.println(DateToStr);
format = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
DateToStr = format.format(curDate);
System.out.println(DateToStr);
format = new SimpleDateFormat("dd MMMM yyyy zzzz", Locale.ENGLISH);
DateToStr = format.format(curDate);
System.out.println(DateToStr);
format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
DateToStr = format.format(curDate);
System.out.println(DateToStr);
try {
Date strToDate = format.parse(DateToStr);
System.out.println(strToDate);
} catch (ParseException e) {
e.printStackTrace();
}
}

- 144
- 1
- 2
- 9