Need to get current timestamp in Java

I need to get the current timestamp in Java, with the format MM/DD/YYYY h:mm:ss AM/PM ,

For example: 06/01/2000 10:01:50 AM

I also need to be Threadsafe.

Can I use something like this?

 java.util.Date date = new java.util.Date(); System.out.println(new Timestamp(date.getTime())); 

Or the examples discussed by reference here .

+48
java datetime timestamp format
Dec 01 '11 at 16:45
source share
10 answers

A problem in the SimpleDateFormat stream should not be a problem if you just create it inside the same method block as you use it. In other words, you do not assign it as a static or instance class variable and reuse it in one or more methods that can be called by multiple threads. Only in this way will the SimpleDateFormat stream protection be set. However, you can safely reuse the same instance of SimpleDateFormat in the same method block that it could access only on the current thread.

In addition, the java.sql.Timestamp class that you use there should not be abused because it is specific to the JDBC API in order to be able to store or retrieve the TIMESTAMP / DATETIME column TIMESTAMP in the SQL database and convert it from / to java.util.Date .

So this should do:

 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a"); String formattedDate = sdf.format(date); System.out.println(formattedDate); // 12/01/2011 4:48:16 PM 
+69
Dec 01 '11 at 16:48
source share
β€” -

Print the timestamp in java using java.sql.Timestamp.

 import java.sql.Timestamp; import java.util.Date; public class GetCurrentTimeStamp { public static void main( String[] args ){ java.util.Date date= new java.util.Date(); System.out.println(new Timestamp(date.getTime())); } } 

Fingerprints:

 2014-08-07 17:34:16.664 

Print the timestamp in Java using SimpleDateFormat on a single layer.

 import java.util.Date; import java.text.SimpleDateFormat; class Runner{ public static void main(String[] args){ System.out.println( new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date())); } } 

Print

 08/14/2014 14:10:38 

Java date format designation:

 G Era designation Text AD y Year Year 1996; 96 M Month in year Month July; Jul; 07 w Week in year Number 27 W Week in month Number 2 D Day in year Number 189 d Day in month Number 10 F Day of week in month Number 2 E Day in week Text Tuesday; Tue a Am/pm marker Text PM H Hour in day (0-23) Number 0 k Hour in day (1-24) Number 24 K Hour in am/pm (0-11) Number 0 h Hour in am/pm (1-12) Number 12 m Minute in hour Number 30 s Second in minute Number 55 S Millisecond Number 978 z Time zone General time zone Pacific Standard Time; PST; GMT-08:00 Z Time zone RFC 822 time zone -0800 
+13
Aug 07 '14 at 21:37
source share

Try a one line solution:

 import java.util.Date; String timestamp = new java.text.SimpleDateFormat("MM/dd/yyyy h:mm:ss a").format(new Date()); 
+7
Nov 28 '13 at 11:47
source share

The fact that SimpleDateFormat not thread safe does not mean that you cannot use it. This only means that you should not use one (potentially, but not necessarily static ) instance that accesses from multiple threads at the same time.

Instead, just create a fresh SimpleDateFormat for each thread. Instances created as local variables within a method are safe by definition because they cannot be reached from any parallel threads.

You might want to take a look at the ThreadLocal class, although I would recommend just creating a new instance where you need it. Of course, you can define a format defined as static final String DATE_FORMAT_PATTERN = "..."; , and use it for each new instance.

+5
Dec 01 '11 at 16:52
source share

java.time

With Java 8+, you can use the java.time package . In particular, use DateTimeFormatterBuilder and DateTimeFormatter to format patterns and literals.

 DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendPattern("MM").appendLiteral("/") .appendPattern("dd").appendLiteral("/") .appendPattern("yyyy").appendLiteral(" ") .appendPattern("hh").appendLiteral(":") .appendPattern("mm").appendLiteral(":") .appendPattern("ss").appendLiteral(" ") .appendPattern("a") .toFormatter(); System.out.println(LocalDateTime.now().format(formatter)); 

Exit...

 06/22/2015 11:59:14 AM 

Or if you need a different time zone ...

 // system default System.out.println(formatter.withZone(ZoneId.systemDefault()).format(Instant.now())); // Chicago System.out.println(formatter.withZone(ZoneId.of("America/Chicago")).format(Instant.now())); // Kathmandu System.out.println(formatter.withZone(ZoneId.of("Asia/Kathmandu")).format(Instant.now())); 

Exit...

 06/22/2015 12:38:42 PM 06/22/2015 02:08:42 AM 06/22/2015 12:53:42 PM 
+3
Jun 22 '15 at 6:39
source share

Joda time

Here is the same code, but using a third-party Joda-Time 2.3 library.

In real life, I would indicate the time zone, since using the default zone is usually bad practice. But here the simplicity of the example is omitted.

 org.joda.time.DateTime now = new DateTime(); org.joda.time.format.DateTimeFormatter formatter = DateTimeFormat.forPattern( "MM/dd/yyyy h:mm:ss a" ); String nowAsString = formatter.print( now ); System.out.println( "nowAsString: " + nowAsString ); 

At startup ...

 nowAsString: 11/28/2013 11:28:15 PM 
+1
Nov 29 '13 at 7:31
source share

I did it like that when I wanted tmiestamp

  String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime()); 

Hope this helps :) As a newbie, I think this is self-evident

I also need to import java.text.SimpleDateFormat; heading for work :))

+1
Sep 12 '16 at 10:04 on
source share

You can use java.util.Date with direct format:

 String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date()); 
+1
Jun 14 '17 at 14:24
source share
 String.format("{0:dddd, MMMM d, yyyy hh:mm tt}", dt); 
0
Dec 01 '11 at 16:55
source share

Well, sometimes it's also helpful.

 import java.util.Date; public class DisplayDate { public static void main(String args[]) { // Instantiate an object Date date = new Date(); // display time and date System.out.println(date.toString());}} 

sample output: Mon Jul 03 19:07:15 IST 2017

0
Jul 03 '17 at 13:35
source share



All Articles