如何获取Java当前时刻的年,月,日,小时,分,秒,毫秒? [英] How to get year, month, day, hours, minutes, seconds and milliseconds of the current moment in Java?
问题描述
如何获取Java当前时刻的年,月,日,时,分,秒,毫秒?我想把它们作为 Strings
。
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
.
推荐答案
你可以使用getHttp://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html\" rel=\"noreferrer\"> java.time的getter。 LocalDateTime
。
You can use the getters of java.time.LocalDateTime
for that.
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);
或者,当您还没有使用Java 8时,请使用 java.util.Calendar
。
Or, when you're not on Java 8 yet, make use of 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);
无论哪种方式,都打印到现在:
Either way, this prints as of now:
2010-04-16 15:15:17.816
要转换 int
to String
,使用 String#valueOf()
。
To convert an int
to String
, make use of String#valueOf()
.
如果您的意图是所有 以安排并以人性化的字符串格式显示,那么更好地使用Java8的< a href =https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html =noreferrer> java.time.format。 DateTimeFormatter
(本教程),
If your intent is after all to arrange and display them in a human friendly string format, then better use either Java8's 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);
或者当您不在Java 8上时,请使用 java.text.SimpleDateFormat
:
or when you're not on Java 8 yet, 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);
无论哪种方式,都会产生:
Either way, this yields:
2010-04-16T15:15:17.816
Fri, 16 Apr 2010 15:15:17 GMT
20100416151517