在 Java 中以特定格式打印日期时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40715424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing out datetime in a specific format in Java?
提问by Farheen
I want to print out datetime in java in a specific format. I have this C# code which prints out the datetime in this format.
我想以特定格式在java中打印出日期时间。我有这个 C# 代码,它以这种格式打印出日期时间。
DateTime value = new DateTime(2010, 1, 18);
Console.WriteLine(value);
Console.WriteLine(value == DateTime.Today);
The result is - 1/18/2010 12:00:00 AM
结果是 - 1/18/2010 12:00:00 AM
Now, I want to write a java code that prints out the datetime in the same format. I used the joda.time library. This is what I tried so far.
现在,我想编写一个 java 代码,以相同的格式打印出日期时间。我使用了 joda.time 库。这是我到目前为止所尝试的。
DateTime today = new DateTime();
System.out.println(today.toString(“yyyy-MMM-dd”));
How can I pass the year,month and day as the constructor in the DateTime in java and print out in the above format.
如何在java中的DateTime中将年月日作为构造函数传递并以上述格式打印出来。
回答by Wasi Ahmad
Approach 1: Using java.time.LocalDateTime. (Strongly Preferred)
方法 1:使用java.time.LocalDateTime。(强烈推荐)
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println(dtf.format(now)); //2016/11/16 12:08:43
Approach 2: Using java.util.Date.
方法 2:使用java.util.Date。
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date)); //2016/11/16 12:08:43
Approach 3: Using java.util.Calendar.
方法 3:使用java.util.Calendar。
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal)); //2016/11/16 12:08:43
回答by SachinSarawgi
If you need date in 24 hour system then use this approach
如果您需要 24 小时制的日期,请使用此方法
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date custDate = new Date();
System.out.println(sdf.format(custDate));
Please note in 24 hour system there is no need to show AM/PM.
请注意,在 24 小时制中,无需显示 AM/PM。
If you want date in 12 hour system then use below approach
如果您想要 12 小时制的日期,请使用以下方法
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Date custDate = new Date();
System.out.println(sdf.format(custDate));
"a" in the date format will help to show AM/PM.
日期格式中的“a”将有助于显示 AM/PM。
Please import below classes for above code to work
请导入以下类以使上述代码工作
java.text.SimpleDateFormat
java.util.Date
java.text.SimpleDateFormat
java.util.Date
回答by Vasilis Vasilatos
LocalDate.of(2010, 1, 18).atStartOfDay().format(DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss a"))
or
或者
LocalDate.of(2010, 1, 18).atTime(12, 0, 0).format(DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss a"));
if you want to add the time too
如果你也想添加时间