I need to get the Date of the current time with the following format "yyyy-MM-dd'T'HH:mm:ss"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
I know how to format a Date using SimpleDateFormat. At the end I get a String. But I need to get the formatted Date, not String, and as far as I know, I cannot convert back a String to a Date, can I?
If I just return the Date, it is a Date but it is not properly formatted:
Timestamp ts = new Timestamp(new Date().getTime());
EDIT: Unfortunately I need to get the outcome as a Date, not a String, so I cannot use sdf.format(New Date().getTime()) as this returns a String. Also, the Date I need to return is the Date of the current time, not from a static String. I hope that clarifies
Date
is just a moment in time, it does not have a format. - Henrynew Date()
is already aDate
, not aString
. - Alex Salauyounew Date()
. As I already commented earlier, aDate
object does not carry a format. Formats only come into play when you convert theDate
into aString
. - Henryjava.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes. Much of the java.time functionality is back-ported to Java 6 & Java 7 in the ThreeTen-Backport project. Further adapted for earlier Android in the ThreeTenABP project. See How to use ThreeTenABP…. - Basil Bourque