如何在Java中获得当前时刻的年,月,日,时,分,秒和毫秒?

如何在Java中获得当前时刻的年,月,日,时,分,秒和毫秒? 我想把它们作为Strings

你可以使用java.time.LocalDateTime的getters来实现。

 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

 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); 

无论哪种方式,这个打印到现在:

 2010-04-16 15:15:17.816

要将int转换为String ,请使用String#valueOf()


如果你的意图毕竟是以人性化的string格式进行排列和显示,那么最好使用Java8的java.time.format.DateTimeFormatter ( 这里的教程 ),

 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

 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); 

无论哪种方式,这产生:

 2010-04-16T15:15:17.816
星期五,16四月2010 15:15:17 GMT
 20100416151517

也可以看看:

  • Javastring到date的转换

切换到乔达时间 ,你可以做三行

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

您还可以直接访问该date的各个字段,而无需使用日历。

 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()); 

输出如下:

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

根据http://www.joda.org/joda-time/

Joda-Time是Java的事实上的标准date和时间库。 从Java SE 8开始,用户被要求迁移到java.time(JSR-310)。

  // 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())); 

对于Java 8及更高版本,请使用java.time包 。

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

LocalDateTime.now()是从默认时区的系统时钟返回当前date时间的静态方法。 所有的get方法都返回一个int值。

或者使用java.sql.Timestamp。 日历有点沉重,我build议不要在生产代码中使用它。 乔达更好。

 import java.sql.Timestamp; public class DateTest { /** * @param args */ public static void main(String[] args) { System.out.println(new Timestamp(System.currentTimeMillis())); } } 

在java 7日历一行

 new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(Calendar.getInstance().getTime()) 

查看java.util.Calendar类及其派生类的API文档(您可能对GregorianCalendar类特别感兴趣)。

现在日历=新的日历()/ /或新的GregorianCalendar(),或任何你需要的味道

now.MONTH now.HOUR

等等