43

I want to convert Long value to String or Date in this format dd/mm/YYYY.

I have this value in Long format: 1343805819061.

It is possible to convert it to Date format?

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
HaOx
  • 1,839
  • 6
  • 26
  • 36

6 Answers6

104

You can use below line of code to do this. Here timeInMilliSecond is long value.

 String dateString = new SimpleDateFormat("MM/dd/yyyy").format(new Date(TimeinMilliSeccond));

Or you can use below code too also.

 String longV = "1343805819061";
 long millisecond = Long.parseLong(longV);
 // or you already have long value of date, use this instead of milliseconds variable.
 String dateString = DateFormat.format("MM/dd/yyyy", new Date(millisecond)).toString();

Reference:- DateFormat and SimpleDateFormat

P.S. Change date format according to your need.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • 2
    Just to add, if you are also getting some 1970 date from it, please ensure your timestamp is in miliseconds. A datetimestamp of recent years should have 13 digits in it. I was struggling a lot with this as mine had 10 digits. – Anuj Garg Feb 29 '20 at 18:34
5

You can use method setTime on the Date instance or the contructor Date(long);

setTime(long time) 
      Sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT.

Date(long date) 
      Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT

Then use the simple date formater

see http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/text/DateFormatter.html

Mark Bakker
  • 1,278
  • 8
  • 19
4
java.util.Date dateObj = new java.util.Date(timeStamp);

Here timeStamp is your long integer which is actually timestamp in millieseconds, you get the java date object, now you can convert it into string by this

SimpleDateFormat dateformatYYYYMMDD = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat dateformatMMDDYYYY = new SimpleDateFormat("MMddyyyy");

StringBuilder nowYYYYMMDD = new StringBuilder( dateformatYYYYMMDD.format( dateObj ) );
StringBuilder nowMMDDYYYY = new StringBuilder( dateformatMMDDYYYY.format( dateObj ) );
Adeel Pervaiz
  • 1,336
  • 10
  • 11
3

java.time and ThreeTenABP

I am providing the modern answer. I suggest using java.time, the modern Java date and time API, for your date work. This will work on your Android version:

    // Take Catalan locale as an example for the demonstration
    DateTimeFormatter dateFormatter
            = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
                    .withLocale(Locale.forLanguageTag("ca"));

    long millisecondsSinceEpoch = 1_343_805_819_061L;
    ZonedDateTime dateTime = Instant.ofEpochMilli(millisecondsSinceEpoch)
            .atZone(ZoneId.systemDefault());
    String dateString = dateTime.format(dateFormatter);
    System.out.println("As formatted date: " + dateString);

Output is:

As formatted date: 01/08/2012

I recommend that you use a built-in localized date format for presentation to your user. I took Catalan date format just as an example. Formats for many languages, countries and dialects are built-in.

The SimpleDateFormat class used in most of the old answers is a notorious troublemaker of a class. The Date class also used is poorly designed too. Fortunately they are both long outdated. It’s no longer recommended to use any of those. And I just find java.time so much nicer to work with.

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

if thre is 10 digits in long then try this

long DateInLong= 1584212400;

Date date = new Date(DateInLong*1000L);

SimpleDateFormat simpledateformate= new SimpleDateFormat("yyyy-MM-dd");

String DATE = simpledateformate.format(date);

0
    public String getDuration(long msec) {
        if (msec == 0)
            return "00:00";
        long sec = msec / 1000;
        long min = sec / 60;
        sec = sec % 60;
        String minstr = min + "";
        String secstr = sec + "";
        if (min < 10)
            minstr = "0" + min;
        if (sec < 10)
            secstr = "0" + sec;
        return minstr + ":" + secstr;
    }
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Pouria Hemi Nov 06 '20 at 12:47