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?
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?
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.
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
org.threeten.bp with subpackages.java.time was first described.java.time to Java 6 and 7 (ThreeTen for JSR-310).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);
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;
}