Java 如何获取带有日期、小时和分钟的 ISO 8601 格式的当前时刻?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3914404/
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
How to get current moment in ISO 8601 format with date, hour, and minute?
提问by yegor256
采纳答案by Joachim Sauer
Use SimpleDateFormat
to format any Date
object you want:
使用SimpleDateFormat
格式化任何Date
你想要的对象:
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); // Quoted "Z" to indicate UTC, no timezone offset
df.setTimeZone(tz);
String nowAsISO = df.format(new Date());
Using a new Date()
as shown above will format the current time.
使用new Date()
如上所示的a将格式化当前时间。
回答by Jigar Joshi
use JodaTime
使用JodaTime
The ISO 8601 calendar system is the default implementation within Joda-Time
ISO 8601 日历系统是 Joda-Time 中的默认实现
Hereis the doc for JodaTime Formatter
这是 JodaTime Formatter 的文档
Edit:
编辑:
If you don't want to add or if you don't see value of adding above library you could just use in built SimpleDateFormat
class to format the Date to required ISO format
如果您不想添加或看不到添加上述库的价值,您可以在内置SimpleDateFormat
类中使用将日期格式化为所需的 ISO 格式
as suggested by @Joachim Sauer
正如@Joachim Sauer 所建议的那样
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
String nowAsString = df.format(new Date());
回答by aioobe
Java 8:
爪哇 8:
thisMoment = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mmX")
.withZone(ZoneOffset.UTC)
.format(Instant.now());
Pre Java 8:
Java 8 之前:
thisMoment = String.format("%tFT%<tRZ",
Calendar.getInstance(TimeZone.getTimeZone("Z")));
From the docs:
从文档:
'R'
Time formatted for the 24-hour clock as "%tH:%tM"'F'
ISO 8601 complete date formatted as "%tY-%tm-%td".
'R'
24 小时制的时间格式为“%tH:%tM”'F'
ISO 8601 完整日期格式为“%tY-%tm-%td”。
回答by user85421
For systems where the default Time Zone is not UTC:
对于默认时区不是 UTC 的系统:
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
df.setTimeZone(tz);
String nowAsISO = df.format(new Date());
The SimpleDateFormat
instance may be declared as a global constant if needed frequently, but beware that this class is not thread-safe. It must be synchronized if accessed concurrently by multiple threads.
的SimpleDateFormat
,如果经常需要的情况下可以声明为一个全局常量,但要注意,这个类不是线程安全的。如果多个线程并发访问,则必须同步。
EDIT: I would prefer Joda Time if doing many different Times/Date manipulations...
EDIT2: corrected: setTimeZone
does not accept a String (corrected by Paul)
编辑:如果进行许多不同的时间/日期操作,我更喜欢 Joda Time...
EDIT2:更正:setTimeZone
不接受字符串(由 Paul 更正)
回答by Surej
Try This,
尝试这个,
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZ");
String date=sdf.format (new Date() );
Its For ISO 8601 format
其为 ISO 8601 格式
回答by Matthew Buckett
If you don't want to include Jodatime (as nice as it is)
如果您不想包含 Jodatime(尽管如此)
javax.xml.bind.DatatypeConverter.printDateTime(
Calendar.getInstance(TimeZone.getTimeZone("UTC"))
);
which returns a string of:
它返回一个字符串:
2012-07-10T16:02:48.440Z
which is slightly different to the original request but is still ISO-8601.
这与原始请求略有不同,但仍然是 ISO-8601。
回答by gilbertpilz
Here's a whole class optimized so that invoking "now()" doesn't do anything more that it has to do.
这是一个优化的整个类,因此调用“now()”不会做任何它必须做的事情。
public class Iso8601Util
{
private static TimeZone tz = TimeZone.getTimeZone("UTC");
private static DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
static
{
df.setTimeZone(tz);
}
public static String now()
{
return df.format(new Date());
}
}
回答by yegor256
DateFormatUtilsfrom Apache commons-lang3
have useful constants, for example: DateFormatUtils.ISO_DATETIME_FORMAT
来自 Apache 的DateFormatUtilscommons-lang3
有一些有用的常量,例如:DateFormatUtils.ISO_DATETIME_FORMAT
回答by okwap
ISO 8601 may contains seconds see http://en.wikipedia.org/wiki/ISO_8601#Times
ISO 8601 可能包含秒见http://en.wikipedia.org/wiki/ISO_8601#Times
so the code should be
所以代码应该是
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
回答by d.danailov
For Java version 7
对于 Java 版本 7
You can follow Oracle documentation: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
您可以遵循 Oracle 文档:http: //docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
X - is used for ISO 8601 time zone
X - 用于 ISO 8601 时区
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
df.setTimeZone(tz);
String nowAsISO = df.format(new Date());
System.out.println(nowAsISO);
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
//nowAsISO = "2013-05-31T00:00:00Z";
Date finalResult = df1.parse(nowAsISO);
System.out.println(finalResult);