Java 如何将 Instant 转换为日期格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30593010/
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 convert an Instant to a date format?
提问by hari m
I can convert a java.util.Date
to a java.time.Instant
(Java 8 and later) this way:
我可以通过这种方式将 a 转换java.util.Date
为 a java.time.Instant
(Java 8 及更高版本):
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 8);
cal.set(Calendar.MINUTE, 30);
Date startTime = cal.getTime();
Instant i = startTime.toInstant();
Can anybody let me know about the convert that instant to date with a specific date & time format? i.e 2015-06-02 8:30:00
任何人都可以让我知道使用特定日期和时间格式将即时转换为日期吗?即 2015-06-02 8:30:00
I have gone through api but could not find a satisfactory answer.
我已经通过 api 但找不到满意的答案。
回答by Murat Karag?z
回答by Umberto Raimondi
If you want to convert an Instant
to a Date
:
如果要将 an 转换Instant
为 a Date
:
Date myDate = Date.from(instant);
And then you can use SimpleDateFormat
for the formatting part of your question:
然后您可以将其SimpleDateFormat
用于问题的格式化部分:
SimpleDateFormat formatter = new SimpleDateFormat("dd MM yyyy HH:mm:ss");
String formattedDate = formatter.format(myDate);
回答by assylias
An Instant is what it says: a specific instant in time - it does not have the notion of date and time (the time in New York and Tokyo is not the same at a given instant).
Instant 就是它所说的:一个特定的时间瞬间 - 它没有日期和时间的概念(纽约和东京的时间在给定的瞬间是不同的)。
To print it as a date/time, you first need to decide which timezone to use. For example:
要将其打印为日期/时间,您首先需要决定使用哪个时区。例如:
System.out.println(LocalDateTime.ofInstant(i, ZoneOffset.UTC));
This will print the date/time in iso format: 2015-06-02T10:15:02.325
这将以iso格式打印日期/时间: 2015-06-02T10:15:02.325
If you want a different format you can use a formatter:
如果你想要不同的格式,你可以使用格式化程序:
LocalDateTime datetime = LocalDateTime.ofInstant(i, ZoneOffset.UTC);
String formatted = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss").format(datetime);
System.out.println(formatted);
回答by Saket Mittal
try Parsing and Formatting
尝试解析和格式化
Take an example Parsing
举个例子 解析
String input = ...;
try {
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("MMM d yyyy");
LocalDate date = LocalDate.parse(input, formatter);
System.out.printf("%s%n", date);
}
catch (DateTimeParseException exc) {
System.out.printf("%s is not parsable!%n", input);
throw exc; // Rethrow the exception.
}
Formatting
格式化
ZoneId leavingZone = ...;
ZonedDateTime departure = ...;
try {
DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a");
String out = departure.format(format);
System.out.printf("LEAVING: %s (%s)%n", out, leavingZone);
}
catch (DateTimeException exc) {
System.out.printf("%s can't be formatted!%n", departure);
throw exc;
}
The output for this example, which prints both the arrival and departure time, is as follows:
此示例的输出同时打印到达和离开时间,如下所示:
LEAVING: Jul 20 2013 07:30 PM (America/Los_Angeles)
ARRIVING: Jul 21 2013 10:20 PM (Asia/Tokyo)
For more details check this page- https://docs.oracle.com/javase/tutorial/datetime/iso/format.html
有关更多详细信息,请查看此页面 - https://docs.oracle.com/javase/tutorial/datetime/iso/format.html