Java 将纪元时间转换为日期字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9754600/
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
Converting Epoch time to date string
提问by nexus490
I have seen this question asked multiple times and none of the answers seem to be what i need.
我已经多次看到这个问题,但似乎没有一个答案是我需要的。
I have a long type variable which has an epoch time stored in it.
我有一个长型变量,其中存储了一个纪元时间。
What i want to do is convert it to a String
我想要做的是将其转换为字符串
for example if the epoch time stored was for today the final string would read:
例如,如果存储的纪元时间是今天,则最终字符串将显示为:
17/03/2012
17/03/2012
How would i to this?
我该怎么办?
采纳答案by Reinard
Look into SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.format(new Date(myTimeAsLong));
回答by Jon Skeet
You'd create a Date
from the long
- that's easy:
您可以Date
从long
-创建一个- 这很容易:
Date date = new Date(epochTime);
Note that epochTime
here ought to be in milliseconds since the epoch - if you've got secondssince the epoch, multiply by 1000.
请注意,epochTime
这里应该是自纪元以来的毫秒数 - 如果自纪元以来有秒数,则乘以 1000。
Then you'd create a SimpleDateFormat
specifying the relevant pattern, culture and time zone. For example:
然后,您将创建一个SimpleDateFormat
指定相关模式、文化和时区的文件。例如:
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
format.setTimeZone(...);
Then use that to format the date to a string:
然后使用它将日期格式化为字符串:
String text = format.format(date);
回答by Ajibola
You need to be aware that epoch time in java is in milliseconds, while what you are converting may be in seconds. Ensure that both sides of the conversions are in milliseconds, and then you can fetch the date parameters from the Date object.
您需要注意 java 中的纪元时间以毫秒为单位,而您正在转换的时间可能以秒为单位。确保双方的转换都以毫秒为单位,然后您就可以从 Date 对象中获取日期参数。
回答by Basil Bourque
Joda-Time
乔达时间
If by epoch time you meant a count of milliseconds since first moment of 1970 in UTC, then here is some example code using the Joda-Time library…
如果纪元时间是指自 UTC 时间 1970 年第一时刻以来的毫秒数,那么这里是一些使用 Joda-Time 库的示例代码……
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
DateTime dateTime = new DateTime( yourMilliseconds, timeZone );
String output = DateTimeFormat.forStyle( "S-" ).withLocale( Locale.CANADA_FRENCH ).print( dateTime );
Other Epochs
其他时代
That definition of epoch is common because of its use within Unix. But be aware that at least a couple dozen epoch definitionsare used by various computer systems.
epoch 的定义很常见,因为它在 Unix 中使用。但请注意,各种计算机系统至少使用了几十个纪元定义。
回答by Damith Ganegoda
Date date = new Date(String);
this is deprecated.
这已被弃用。
solution
解决方案
Date date = new Date(1406178443 * 1000L);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(date);
make sure multiply by 1000L
确保乘以 1000L
回答by Silambarasan Poonguti
Try this...
尝试这个...
sample Epoch timestamp is 1414492391238
样本纪元时间戳为 1414492391238
Method:
方法:
public static String GetHumanReadableDate(long epochSec, String dateFormatStr) {
Date date = new Date(epochSec * 1000);
SimpleDateFormat format = new SimpleDateFormat(dateFormatStr,
Locale.getDefault());
return format.format(date);
}
Usability:
可用性:
long timestamp = Long.parseLong(engTime) / 1000;
String engTime_ = GetHumanReadableDate(timestamp, "dd-MM-yyyy HH:mm:ss aa");
Result:
结果:
28-10-2014 16:03:11 pm
28-10-2014 下午 16:03:11
Happy coding
快乐编码
回答by Martin Zeitler
based upon previous answers, while passing system default Locale
and TimeZone
...
基于以前的答案,同时传递系统默认值Locale
和TimeZone
...
String epochToIso8601(long time) {
String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
sdf.setTimeZone(TimeZone.getDefault());
return sdf.format(new Date(time * 1000));
}
回答by Charitha Goonewardena
try this
尝试这个
Date date = new Date(1476126532838L);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(date);
format.setTimeZone(TimeZone.getTimeZone("Asia/Colombo"));//your zone
formatted = format.format(date);
System.out.println(formatted);
回答by Ole V.V.
Time for someone to provide the modern answer (valid and recommended since 2014).
是时候提供现代答案了(自 2014 年起有效并推荐)。
java.time
时间
DateTimeFormatter formatter = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.LONG).withLocale(Locale.US);
String facebookTime = "1548410106047";
long fbt = Long.parseLong(facebookTime);
ZonedDateTime dateTime = Instant.ofEpochMilli(fbt).atZone(ZoneId.of("America/Indiana/Knox"));
System.out.println(dateTime.format(formatter));
The output is:
输出是:
January 25, 2019 at 3:55:06 AM CST
2019 年 1 月 25 日美国中部标准时间上午 3:55:06
If you wanted only the date and in a shorter format, use for example
如果您只想要日期和较短的格式,请使用例如
DateTimeFormatter formatter = DateTimeFormatter
.ofLocalizedDate(FormatStyle.SHORT).withLocale(Locale.US);
1/25/19
19 年 1 月 25 日
Note how the snippet allows you to specify time zone, language (locale) and how long or short of a format you want.
请注意该代码段如何允许您指定时区、语言(区域设置)以及所需格式的长度或长度。
Links
链接
- Oracle tutorial: Date Timeexplaining how to use java.time.
- My example string was taken from this duplicate question
- Oracle 教程:解释如何使用 java.time 的日期时间。
- 我的示例字符串取自这个重复的问题
回答by Ali Musa
ArLiteDTMConv Utility help converting EPOUCH-UNIX Date-Time values, Form EPOUCH-UNIX-To-Date-format and Vise-Versa. You can set the result to a variable and then use the variable in your script or when passing as parameter or introduce in any DB criteria for both Window and Linux. (Download a zip file on this link)
ArLiteDTMConv 实用程序帮助转换 EPOUCH-UNIX 日期时间值、表格 EPOUCH-UNIX-To-Date-format 和 Vise-Versa。您可以将结果设置为一个变量,然后在脚本中使用该变量,或者在作为参数传递或在 Window 和 Linux 的任何数据库标准中引入时使用该变量。(在此链接上下载 zip 文件)