how to convert milliseconds to date format in android?
Solution 1
Just Try this Sample code:-
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Test {
/**
* Main Method
*/
public static void main(String[] args) {
System.out.println(getDate(82233213123L, "dd/MM/yyyy hh:mm:ss.SSS"));
}
/**
* Return date in specified format.
* @param milliSeconds Date in milliseconds
* @param dateFormat Date format
* @return String representing date in specified format
*/
public static String getDate(long milliSeconds, String dateFormat)
{
// Create a DateFormatter object for displaying date in specified format.
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
}
Solution 2
Convert the millisecond value to Date
instance and pass it to the choosen formatter.
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateString = formatter.format(new Date(dateInMillis)));
Solution 3
public static String convertDate(String dateInMilliseconds,String dateFormat) {
return DateFormat.format(dateFormat, Long.parseLong(dateInMilliseconds)).toString();
}
Call this function
convertDate("82233213123","dd/MM/yyyy hh:mm:ss");
Solution 4
tl;dr
Instant.ofEpochMilli( myMillisSinceEpoch ) // Convert count-of-milliseconds-since-epoch into a date-time in UTC (`Instant`).
.atZone( ZoneId.of( "Africa/Tunis" ) ) // Adjust into the wall-clock time used by the people of a particular region (a time zone). Produces a `ZonedDateTime` object.
.toLocalDate() // Extract the date-only value (a `LocalDate` object) from the `ZonedDateTime` object, without time-of-day and without time zone.
.format( // Generate a string to textually represent the date value.
DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) // Specify a formatting pattern. Tip: Consider using `DateTimeFormatter.ofLocalized…` instead to soft-code the formatting pattern.
) // Returns a `String` object.
java.time
The modern approach uses the java.time classes that supplant the troublesome old legacy date-time classes used by all the other Answers.
Assuming you have a long
number of milliseconds since the epoch reference of first moment of 1970 in UTC, 1970-01-01T00:00:00Z…
Instant instant = Instant.ofEpochMilli( myMillisSinceEpoch ) ;
To get a date requires a time zone. For any given moment, the date varies around the globe by zone.
ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = instant.atZone( z ) ; // Same moment, different wall-clock time.
Extract a date-only value.
LocalDate ld = zdt.toLocalDate() ;
Generate a String representing that value using standard ISO 8601 format.
String output = ld.toString() ;
Generate a String in custom format.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
String output = ld.format( f ) ;
Tip: Consider letting java.time automatically localize for you rather than hard-code a formatting pattern. Use the DateTimeFormatter.ofLocalized…
methods.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?
-
Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 brought some minor features and fixes.
-
Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
-
Android
- Later versions of Android (26+) bundle implementations of the java.time classes.
- For earlier Android (<26), the process of API desugaring brings a subset of the java.time functionality not originally built into Android.
- If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….
Solution 5
DateFormat.getDateInstance().format(dateInMS);
![Pattabi Raman](https://i.stack.imgur.com/w9UXW.jpg?s=256&g=1)
Pattabi Raman
Updated on January 25, 2022Comments
-
Pattabi Raman over 2 years
I have milliseconds. I need it to be converted to date format of
example:
23/10/2011
How to achieve it?
-
gaddam nagaraju over 11 yearsWe can do it by using Time Unit..but it doesn't works fine...use this..this will help you..it works fine
-
ralphgabb about 10 yearsit works on over 10 digit long value but not on 6 downward. hour have a default of 4.. ???
-
Amir Hossein Ghasemi almost 10 yearsyou should Use new SimpleDateFormat(dateFormat,Locale.US(or your locale)) instead of new SimpleDateFormat(dateFormat), because it's causes crash due the change default android language
-
Ravindra Rathour almost 8 yearsUse these method to convert date in string format like 2016-08-18 or any type of in string format to DateFormat and you can also convert date into milliseconds.
-
Steve Rogers over 7 yearsThanks Mahmood, this solution requires API Level 1 (my project is lowered to API 15) while other Answer requires API Level 24 (Date and/or Calendar library)
-
behelit over 6 yearswhat if you're american?
-
Basil Bourque over 6 yearsFYI, the troublesome old date-time classes such as
java.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes. Much of the java.time functionality is back-ported to Java 6 & Java 7 in the ThreeTen-Backport project. Further adapted for earlier Android in the ThreeTenABP project. See How to use ThreeTenABP…. -
Aldor almost 4 years@Uttam this works thanks!, But i had a question. Should we receive time and date in this "/Date(1224043200000)/" format? I've read that its an old json format of microsoft and it should not be used in new development.
-
Ole V.V. almost 4 years(1) Please don’t teach the young ones to use the long outdated and notoriously troublesome
SimpleDateFormat
class. At least not as the first option. And not without any reservation. Today we have so much better injava.time
, the modern Java date and time API, and itsDateTimeFormatter
. Yes, you can use it on Android. For older Android use desugaring or see How to use ThreeTenABP in Android Project. -
Ole V.V. almost 4 years(2) I don’t think you intended lower case
hh
? Please check the difference between upper case and lower case here. -
Ole V.V. almost 4 yearsThe modern way is:
return Instant.ofEpochMilli(millis).atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern(DATE_FORMAT, Locale.US))
. Yes, it’s longer because it gives more information about what is going on, so it’s an advantage. -
Rupam Das over 3 years@OleV.V. Call requires API level 26
-
Ole V.V. over 3 years@RupamDas Either use desugaring or add ThreeTenABP to your Android project in order to use java.time, the modern Java date and time API, on older Android versions (under API level 26).
-
Ole V.V. over 3 yearsConsider throwing away the long outmoded and notoriously troublesome
SimpleDateFormat
and friends. See if you either can use desugaring or add ThreeTenABP to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. -
Ole V.V. over 3 yearsAs an aside consider throwing away the long outmoded and notoriously troublesome
SimpleDateFormat
and friends. See if you either can use desugaring or add ThreeTenABP to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. -
Mihae Kheel about 3 years@SteveRogers this will allow backward compatibility developer.android.com/studio/write/…