将整数日期时间转换为实时日期时间问题?JAVA

将整数日期时间转换为实时日期时间问题?JAVA,java,android-studio,date,datetime,datetime-format,Java,Android Studio,Date,Datetime,Datetime Format,因此,我在Java中将整数日期时间格式转换为普通日期时间格式时遇到了问题。 我有一个变量int DateTime,例如:“/Date(1484956800000)/”。我正试图把它转换成正常的日期时间,并显示在屏幕上 我试过这样 String dateAsText = new SimpleDateFormat("MM-dd HH:mm") .format(new Date(Integer.parseInt(deals.getDate_ti

因此,我在Java中将整数日期时间格式转换为普通日期时间格式时遇到了问题。 我有一个变量int DateTime,例如:“/Date(1484956800000)/”。我正试图把它转换成正常的日期时间,并显示在屏幕上

我试过这样

   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);

我建议您停止使用过时且容易出错的
java.util
date-time API和
SimpleDataFormat
。切换到
java.time
date-time API和相应的格式化API(
java.time.format
)。从了解有关现代日期时间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) {
        // 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);
    }
}
输出:

2017-01-21T00:00:00Z
2017-01-21T00:00
01-21 00:00
Sat Jan 21 00:00:00 GMT 2017
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);

        // Custom format
        String dateAsText = new SimpleDateFormat("MM-dd HH:mm").format(date);
        System.out.println(dateAsText);
    }
}
输出:

2017-01-21T00:00:00Z
2017-01-21T00:00
01-21 00:00
Sat Jan 21 00:00:00 GMT 2017
01-21 00:00

您的int/long值(1484956800000)似乎已经是毫秒分辨率的时间戳。试着在不乘以1000L的情况下转换它。@ThomasKläger我删除了1000L,但同样的问题…,同样的问题-我想你忘了告诉我们哪个问题了。很抱歉,我错过了一个:
Integer.parseInt(deals.getDate\u time())
将为1484956800000抛出一个
NumberFormatException
,因为1484956800000不符合整数范围。您还必须将其替换为
Long.parseLong(deals.getDate\u time())