How to get the year, month, day, hours, minutes, seconds and milliseconds of the current moment in Java?

How can I get the year, month, day, hours, minutes, seconds, and milliseconds of the current moment in Java? I would like to have them as Strings .

+78
java date
Apr 16 '10 at 15:10
source share
8 answers

You can use getters java.time.LocalDateTime .

 LocalDateTime now = LocalDateTime.now(); int year = now.getYear(); int month = now.getMonthValue(); int day = now.getDayOfMonth(); int hour = now.getHour(); int minute = now.getMinute(); int second = now.getSecond(); int millis = now.get(ChronoField.MILLI_OF_SECOND); // Note: no direct getter available. System.out.printf("%d-%02d-%02d %02d:%02d:%02d.%03d", year, month, day, hour, minute, second, millis); 

Or, if you are not already in Java 8, use java.util.Calendar .

 Calendar now = Calendar.getInstance(); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH) + 1; // Note: zero based! int day = now.get(Calendar.DAY_OF_MONTH); int hour = now.get(Calendar.HOUR_OF_DAY); int minute = now.get(Calendar.MINUTE); int second = now.get(Calendar.SECOND); int millis = now.get(Calendar.MILLISECOND); System.out.printf("%d-%02d-%02d %02d:%02d:%02d.%03d", year, month, day, hour, minute, second, millis); 

In any case, this is printing at the moment:

 2010-04-16 15: 15: 17.816

To convert int to String , use String#valueOf() .




If your intention is to arrange and display them in a friendly human format, then it is better to use either Java8 java.time.format.DateTimeFormatter ( tutorial here ),

 LocalDateTime now = LocalDateTime.now(); String format1 = now.format(DateTimeFormatter.ISO_DATE_TIME); String format2 = now.atZone(ZoneId.of("GMT")).format(DateTimeFormatter.RFC_1123_DATE_TIME); String format3 = now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss", Locale.ENGLISH)); System.out.println(format1); System.out.println(format2); System.out.println(format3); 

or when you are not already in Java 8, use java.text.SimpleDateFormat :

 Date now = new Date(); // java.util.Date, NOT java.sql.Date or java.sql.Timestamp! String format1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ENGLISH).format(now); String format2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH).format(now); String format3 = new SimpleDateFormat("yyyyMMddHHmmss", Locale.ENGLISH).format(now); System.out.println(format1); System.out.println(format2); System.out.println(format3); 

In any case, this gives:

 2010-04-16T15: 15: 17.816
 Fri, 16 Apr 2010 15:15:17 GMT
 20100416151517

See also:

  • Convert strings to string in format
+201
Apr 16 '10 at 15:13
source share

Go to joda-time and you can do it in three lines

 DateTime jodaTime = new DateTime(); DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss.SSS"); System.out.println("jodaTime = " + formatter.print(jodaTime)); 

You also have direct access to individual date fields without using a Calendar.

 System.out.println("year = " + jodaTime.getYear()); System.out.println("month = " + jodaTime.getMonthOfYear()); System.out.println("day = " + jodaTime.getDayOfMonth()); System.out.println("hour = " + jodaTime.getHourOfDay()); System.out.println("minute = " + jodaTime.getMinuteOfHour()); System.out.println("second = " + jodaTime.getSecondOfMinute()); System.out.println("millis = " + jodaTime.getMillisOfSecond()); 

The output is as follows:

 jodaTime = 2010-04-16 18:09:26.060 year = 2010 month = 4 day = 16 hour = 18 minute = 9 second = 26 millis = 60 

According to http://www.joda.org/joda-time/

Joda-Time is the actual standard date and time library for Java. Starting with Java SE 8, users are encouraged to upgrade to java.time (JSR-310).

+29
Apr 16 '10 at 16:11
source share
  // Java 8 System.out.println(LocalDateTime.now().getYear()); // 2015 System.out.println(LocalDateTime.now().getMonth()); // SEPTEMBER System.out.println(LocalDateTime.now().getDayOfMonth()); // 29 System.out.println(LocalDateTime.now().getHour()); // 7 System.out.println(LocalDateTime.now().getMinute()); // 36 System.out.println(LocalDateTime.now().getSecond()); // 51 System.out.println(LocalDateTime.now().get(ChronoField.MILLI_OF_SECOND)); // 100 // Calendar System.out.println(Calendar.getInstance().get(Calendar.YEAR)); // 2015 System.out.println(Calendar.getInstance().get(Calendar.MONTH ) + 1); // 9 System.out.println(Calendar.getInstance().get(Calendar.DAY_OF_MONTH)); // 29 System.out.println(Calendar.getInstance().get(Calendar.HOUR_OF_DAY)); // 7 System.out.println(Calendar.getInstance().get(Calendar.MINUTE)); // 35 System.out.println(Calendar.getInstance().get(Calendar.SECOND)); // 32 System.out.println(Calendar.getInstance().get(Calendar.MILLISECOND)); // 481 // Joda Time System.out.println(new DateTime().getYear()); // 2015 System.out.println(new DateTime().getMonthOfYear()); // 9 System.out.println(new DateTime().getDayOfMonth()); // 29 System.out.println(new DateTime().getHourOfDay()); // 7 System.out.println(new DateTime().getMinuteOfHour()); // 19 System.out.println(new DateTime().getSecondOfMinute()); // 16 System.out.println(new DateTime().getMillisOfSecond()); // 174 // Formatted // 2015-09-28 17:50:25.756 System.out.println(new Timestamp(System.currentTimeMillis())); // 2015-09-28T17:50:25.772 System.out.println(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ENGLISH).format(new Date())); // Java 8 // 2015-09-28T17:50:25.810 System.out.println(LocalDateTime.now()); // joda time // 2015-09-28 17:50:25.839 System.out.println(DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss.SSS").print(new org.joda.time.DateTime())); 
+4
Sep 28 '15 at 15:51
source share

With Java 8 and later, use the java.time package .

 LocalDateTime.now().getYear(); LocalDateTime.now().getMonthValue(); LocalDateTime.now().getDayOfMonth(); LocalDateTime.now().getHour(); LocalDateTime.now().getMinute(); LocalDateTime.now().getSecond(); 

LocalDateTime.now() is a static method that returns the current date-date from the system clock to the default time zone. All get methods return an int value.

+3
Jun 15 '15 at 15:32
source share

Or use java.sql.Timestamp. The calendar is very difficult, I would recommend using it in the production code. Joda is better.

 import java.sql.Timestamp; public class DateTest { /** * @param args */ public static void main(String[] args) { System.out.println(new Timestamp(System.currentTimeMillis())); } } 
+1
Apr 16 '10 at 17:08
source share

in java 7 Calendar per line

 new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(Calendar.getInstance().getTime()) 
0
May 22 '17 at 9:27 a.m.
source share

Look at the API documentation for the java.util.Calendar class and its derivatives (you may be interested only in the GregorianCalendar class).

-2
Apr 16 '10 at 15:12
source share

Calendar now = new Calendar () // or new GregorianCalendar () or whatever taste you need

now.MONTH now.HOUR

and etc.

-2
Apr 16 '10 at 16:53
source share



All Articles