将日期串成纪元时间
String date into Epoch time
我对日期有点困惑。我目前正在开发天气应用程序并且一切正常..我只想将这种类型的格式处理成我自己想要的格式。
2017-09-10T18:35:00+05:00
我只是想将这个日期转换成大纪元时间,然后以我想要的格式设置日期::
对于 J-SON
或者我想将这个日期转换成更小的数字,即 9 月 9 日星期日 9:23 上午等
使用SimpleDateFormat
实例将字符串解析为Date
对象:
DateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
Date date = parser.parse("2017-09-10T18:35:00+05:00");
然后使用另一个 SimpleDateFormat 来显示它:
DateFormat format = new SimpleDateFormat("EEE, dd MMMMM h:mm a");
String formatted = format.format(date); // Sun, 10 September 1:35 PM
您可以使用 SimpleDate 格式化程序将日期作为字符串解析为纪元
String input = "2017-09-10T18:35:00+05:00";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
try {
Date date = sf.parse(input);
long dateInEpochFormatInMilliSeconds = date.getTime();
//if you want this in seconds then
long dateInEpochFormatInSeconds = date.getTime()/1000L;
//if you want to show only date month and year then
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String date = sdf.format(dateInEpochFormatInMilliSeconds);
//This date String will contain the date in dd-MM-yyyy format
} catch (ParseException| ArithmeticException e) {
e.printStackTrace();
}
String time_at_which_weather_capture = "Time : ";
DateFormat dateFormat = new SimpleDateFormat("EEE,d M yyyy h:MM a");
long timeInMillieSec = 0 ;
try {
Date date = dateFormat.parse(readyToUpdate.getTime());
timeInMillieSec = date.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
time.setText(time_at_which_weather_capture + String.valueOf(time_fetcher(timeInMillieSec)));
public String time_fetcher (long time_coming_to_its_original_form) {
Date date = new Date (time_coming_to_its_original_form);
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d M yyyy h:MM a");
return sdf.format(date);
}
三十ABP
其他答案是正确的,但在写之前就已经过时了。最近,我建议您使用称为 JSR-310 或 java.time
的现代 Java 日期和时间 API。您的日期时间字符串格式是 ISO 8601,现代 类“理解”为默认格式。
你可以在 Android 上使用现代 API 了吗?当然! JSR-310 类 已被移植到 ThreeTenABP 项目中的 Android。所有细节都在 .
long epochTime = OffsetDateTime.parse("2017-09-10T18:35:00+05:00")
.toInstant()
.getEpochSecond();
结果是1505050500。
如何将其转换为人类可读的日期和时间的示例:
String formattedDateTime = Instant.ofEpochSecond(epochTime)
.atZone(ZoneId.of("Africa/Lusaka"))
.format(DateTimeFormatter.ofPattern("EEE, d MMMM h:mm a", Locale.ENGLISH));
这会产生 Sun, 10 September 3:35 PM
。请为您想要的时区 ID 提供正确的区域和城市。如果您想依赖设备的时区设置,请使用 ZoneId.systemDefault()
。请参阅 the documentation of DateTimeFormatter.ofPattern() for the letters you may use in the format pattern string, or use DateTimeFormatter.ofLocalizedDateTime()
了解您的区域设置的一种默认格式。
我对日期有点困惑。我目前正在开发天气应用程序并且一切正常..我只想将这种类型的格式处理成我自己想要的格式。
2017-09-10T18:35:00+05:00
我只是想将这个日期转换成大纪元时间,然后以我想要的格式设置日期::
对于 J-SON
或者我想将这个日期转换成更小的数字,即 9 月 9 日星期日 9:23 上午等
使用SimpleDateFormat
实例将字符串解析为Date
对象:
DateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
Date date = parser.parse("2017-09-10T18:35:00+05:00");
然后使用另一个 SimpleDateFormat 来显示它:
DateFormat format = new SimpleDateFormat("EEE, dd MMMMM h:mm a");
String formatted = format.format(date); // Sun, 10 September 1:35 PM
您可以使用 SimpleDate 格式化程序将日期作为字符串解析为纪元
String input = "2017-09-10T18:35:00+05:00";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
try {
Date date = sf.parse(input);
long dateInEpochFormatInMilliSeconds = date.getTime();
//if you want this in seconds then
long dateInEpochFormatInSeconds = date.getTime()/1000L;
//if you want to show only date month and year then
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String date = sdf.format(dateInEpochFormatInMilliSeconds);
//This date String will contain the date in dd-MM-yyyy format
} catch (ParseException| ArithmeticException e) {
e.printStackTrace();
}
String time_at_which_weather_capture = "Time : ";
DateFormat dateFormat = new SimpleDateFormat("EEE,d M yyyy h:MM a");
long timeInMillieSec = 0 ;
try {
Date date = dateFormat.parse(readyToUpdate.getTime());
timeInMillieSec = date.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
time.setText(time_at_which_weather_capture + String.valueOf(time_fetcher(timeInMillieSec)));
public String time_fetcher (long time_coming_to_its_original_form) {
Date date = new Date (time_coming_to_its_original_form);
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d M yyyy h:MM a");
return sdf.format(date);
}
三十ABP
其他答案是正确的,但在写之前就已经过时了。最近,我建议您使用称为 JSR-310 或 java.time
的现代 Java 日期和时间 API。您的日期时间字符串格式是 ISO 8601,现代 类“理解”为默认格式。
你可以在 Android 上使用现代 API 了吗?当然! JSR-310 类 已被移植到 ThreeTenABP 项目中的 Android。所有细节都在
long epochTime = OffsetDateTime.parse("2017-09-10T18:35:00+05:00")
.toInstant()
.getEpochSecond();
结果是1505050500。
如何将其转换为人类可读的日期和时间的示例:
String formattedDateTime = Instant.ofEpochSecond(epochTime)
.atZone(ZoneId.of("Africa/Lusaka"))
.format(DateTimeFormatter.ofPattern("EEE, d MMMM h:mm a", Locale.ENGLISH));
这会产生 Sun, 10 September 3:35 PM
。请为您想要的时区 ID 提供正确的区域和城市。如果您想依赖设备的时区设置,请使用 ZoneId.systemDefault()
。请参阅 the documentation of DateTimeFormatter.ofPattern() for the letters you may use in the format pattern string, or use DateTimeFormatter.ofLocalizedDateTime()
了解您的区域设置的一种默认格式。