英文:
Convert integer date time into real date time problem? JAVA
问题
所以我有一个在Java中将整数日期时间格式转换为常规日期时间格式的问题。
我有这个变量int DateTime,例如:"/Date(1484956800000)/"。我正在尝试将其转换为常规日期时间并显示在屏幕上...
我尝试了这样做...
String dateAsText = new SimpleDateFormat("MM-dd HH:mm")
.format(new Date(Integer.parseInt(deals.getDate_time()) * 1000L));
// 使用字符串dateAsText设置我的textView
holder.Time.setText(dateAsText);
英文:
so I have this problem converting Integer DateTime format to normal DateTime format in Java.
I have this this variable int DateTime, for example it is : "/Date(1484956800000)/" . And i am trying to convert it to normal date time and show it to the screen ...
I tried like this..
String dateAsText = new SimpleDateFormat("MM-dd HH:mm")
.format(new Date(Integer.parseInt(deals.getDate_time()) * 1000L));
// setting my textView with the string dateAsText
holder.Time.setText(dateAsText);
答案1
得分: 1
我建议您停止使用过时且容易出错的java.util
日期时间API和SimpleDateFormat
。改用现代的java.time
日期时间API和相应的格式化API(java.time.format
)。从**Trail: Date Time**了解有关现代日期时间API的更多信息。
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// 从纪元时间1970-01-01T00:00:00Z起,使用毫秒获取Instant实例
Instant instant = Instant.ofEpochMilli(1484956800000L);
System.out.println(instant);
// 指定时区
ZoneId myTimeZone = ZoneId.of("Europe/London");
// 从Instant获取ZonedDateTime
ZonedDateTime zdt = instant.atZone(myTimeZone);
// 从ZonedDateTime获取LocalDateTime
// 请注意,LocalDateTime会丢弃时区的重要信息
LocalDateTime ldt = zdt.toLocalDateTime();
System.out.println(ldt);
// 自定义格式
String dateAsText = ldt.format(DateTimeFormatter.ofPattern("MM-dd HH:mm"));
System.out.println(dateAsText);
}
}
输出:
2017-01-21T00:00:00Z
2017-01-21T00:00
01-21 00:00
如果您仍然想要使用设计不佳的遗留java.util.Date
,可以按以下方式进行:
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date(1484956800000L);
System.out.println(date);
// 自定义格式
String dateAsText = new SimpleDateFormat("MM-dd HH:mm").format(date);
System.out.println(dateAsText);
}
}
输出:
Sat Jan 21 00:00:00 GMT 2017
01-21 00:00
英文:
I suggest you stop using the outdated and error-prone java.util
date-time API and SimpleDateFormat
. Switch to the modern java.time
date-time API and the corresponding formatting API (java.time.format
). Learn more about the modern date-time API from Trail: Date Time.
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// Obtain an instance of Instant using milliseconds from the epoch of
// 1970-01-01T00:00:00Z
Instant instant = Instant.ofEpochMilli(1484956800000L);
System.out.println(instant);
// Specify the time-zone
ZoneId myTimeZone = ZoneId.of("Europe/London");
// Obtain ZonedDateTime out of Instant
ZonedDateTime zdt = instant.atZone(myTimeZone);
// Obtain LocalDateTime out of ZonedDateTime
// Note that LocalDateTime throws away the important information of time-zone
LocalDateTime ldt = zdt.toLocalDateTime();
System.out.println(ldt);
// Custom format
String dateAsText = ldt.format(DateTimeFormatter.ofPattern("MM-dd HH:mm"));
System.out.println(dateAsText);
}
}
Output:
2017-01-21T00:00:00Z
2017-01-21T00:00
01-21 00:00
If you still want to use the poorly designed legacy java.util.Date
, you can do it as follows:
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date(1484956800000L);
System.out.println(date);
// Custom format
String dateAsText = new SimpleDateFormat("MM-dd HH:mm").format(date);
System.out.println(dateAsText);
}
}
Output:
Sat Jan 21 00:00:00 GMT 2017
01-21 00:00
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论