How to get timestamp in string format in Java? "yyyy.MM.dd.HH.mm.ss"
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Timestamp());
This is what I have, but Timestamp() requires an parameters...
How to get timestamp in string format in Java? "yyyy.MM.dd.HH.mm.ss"
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Timestamp());
This is what I have, but Timestamp() requires an parameters...
Replace
new Timestamp();
with
new java.util.Date()
because there is no default constructor for Timestamp
, or you can do it with the method:
new Timestamp(System.currentTimeMillis());
Use java.util.Date
class instead of Timestamp.
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
This will get you the current date in the format specified.
You can make use of java.util.Date instead of Timestamp :
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
Use only modern java.time classes. Never use the terrible legacy classes such as SimpleDateFormat
, Date
, or java.sql.Timestamp
.
ZonedDateTime // Represent a moment as perceived in the wall-clock time used by the people of a particular region ( a time zone).
.now( // Capture the current moment.
ZoneId.of( "Africa/Tunis" ) // Specify the time zone using proper Continent/Region name. Never use 3-4 character pseudo-zones such as PDT, EST, IST.
) // Returns a `ZonedDateTime` object.
.format( // Generate a `String` object containing text representing the value of our date-time object.
DateTimeFormatter.ofPattern( "uuuu.MM.dd.HH.mm.ss" )
)
The modern approach uses the java.time classes as seen above.
If your JDBC driver complies with JDBC 4.2, you can directly exchange java.time objects with the database. Use PreparedStatement::setObject
and ResultSet::getObject
.
If your JDBC driver does not yet comply with JDBC 4.2 for support of java.time types, you must fall back to using the java.sql classes.
The java.sql types, such as java.sql.Timestamp
, should only be used for transfer in and out of the database. Immediately convert to java.time types in Java 8 and later.
java.time.Instant
A java.sql.Timestamp
maps to a java.time.Instant
, a moment on the timeline in UTC.
java.sql.Timestamp ts = myResultSet.getTimestamp( … );
Instant instant = ts.toInstant();
Apply the desired/expected time zone to get a ZonedDateTime
.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );
Use a DateTimeFormatter
to generate your string. The pattern codes are similar to those of java.text.SimpleDateFormat
but not exactly, so read the doc carefully.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "uuuu.MM.dd.HH.mm.ss" );
String output = zdt.format( formatter );
This particular format is ambiguous as to its exact meaning as it lacks any indication of offset-from-UTC or time zone.
If you have any say in the matter, I suggest you consider using standard ISO 8601 formats rather than rolling your own. The standard format is quite similar to yours. For example:2016-02-20T03:26:32+05:30
.
The java.time classes use these standard formats by default, so no need to specify a pattern. The ZonedDateTime
class extends the standard format by appending the name of the time zone (a wise improvement).
String output = zdt.toString(); // Example: 2007-12-03T10:15:30+01:00[Europe/Paris]
You can convert from java.time back to java.sql.Timestamp
. Extract an Instant
from the ZonedDateTime
.
New methods have been added to the old classes to facilitate converting to/from java.time classes.
java.sql.Timestamp ts = java.sql.Timestamp.from( zdt.toInstant() );
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.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
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.
A more appropriate approach is to specify a Locale region as a parameter in the constructor. The example below uses a US Locale region. Date formatting is locale-sensitive and uses the Locale to tailor information relative to the customs and conventions of the user's region Locale (Java Platform SE 7)
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss", Locale.US).format(new Date());
You can use the following
new java.sql.Timestamp(System.currentTimeMillis()).getTime()
Result : 1539594988651
Hope this will help. Just my suggestion and not for reward points.
Use below code to get current timestamps:
Timestamp ts = new Timestamp(date.getTime());
For reference
How to get current timestamps in Java