-1

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);
archi noris
  • 39
  • 2
  • 6
  • 1
    It seems that your int/long value (1484956800000) already is a timestamp with milliseconds resolution. Try to convert it without multiplying by 1000L first. – Thomas Kläger Aug 17 '20 at 13:53
  • @ThomasKläger i removed the 1000L , but the same problem.., – archi noris Aug 17 '20 at 14:07
  • *the same problem* — which problem, I think you forgot to tell us? [I downvoted because without a proper and precise problem description we cannot help you](http://idownvotedbecau.se/itsnotworking/). – Ole V.V. Aug 17 '20 at 18:30
  • Sorry, I missed that one: `Integer.parseInt(deals.getDate_time())` will throw a `NumberFormatException` for 1484956800000 because 1484956800000 doesn't fit into the integer range. You must also replace it with `Long.parseLong(deals.getDate_time())` – Thomas Kläger Aug 17 '20 at 18:39

1 Answers1

1

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
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110