how to convert milliseconds to date format in android?
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());
}
}
Converting milliseconds to Date object
try my code if you a
long currentTime = System.currentTimeMillis();
TimeZone tz = TimeZone.getDefault();
Calendar cal = GregorianCalendar.getInstance(tz);
int offsetInMillis = tz.getOffset(cal.getTimeInMillis());
currentTime -= offsetInMillis;
Date date = new Date(currentTime);
it is work for me
Android : Convert millisecond to time
You could use SimpleDateFormat
, but be aware that you should set both the time zone and the locale appropriately:
DateFormat formatter = new SimpleDateFormat("HH:mm:ss", Locale.US);
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
String text = formatter.format(new Date(millis));
The time zone part is important, as otherwise it will use the system-default time zone, which would usually be inappropriate. Note that the Date
here will be on January 1st 1970, UTC - assuming your millisecond value is less than 24 hours.
how to get date from milliseconds in android
you can use like this
Calendar cl = Calendar.getInstance();
cl.setTimeInMillis(milliseconds); //here your time in miliseconds
String date = "" + cl.get(Calendar.DAY_OF_MONTH) + ":" + cl.get(Calendar.MONTH) + ":" + cl.get(Calendar.YEAR);
String time = "" + cl.get(Calendar.HOUR_OF_DAY) + ":" + cl.get(Calendar.MINUTE) + ":" + cl.get(Calendar.SECOND);
How to change milliseconds to yyyy-MM-dd'T'HH:mm:ssS format?
you have seconds
not milliseconds
you should multipy your seconds * 1000
Android converting the time in milliseconds
To understand what's happening:
Calendar.getInstance()
creates aCalendar
with the current date/time in the JVM default timezone- You set the hour, minutes and seconds (to
01:30:00
I suppose) - You call
getTimeInMillis()
, that returns the number of milliseconds since unix epoch (1970-01-01T00:00Z
)
Then you use this milliseconds value with TimeUnit
, but that's not what you need. TimeUnit
will treat the number of milliseconds as an amount of time, but what you need is a time of the day:
- a time of the day is a specific moment in a day, such as "10 AM" or "05:30 PM".
- an amount of time is just, well, an amount of elapsed time, such as "10 minutes and 45 seconds", or "2 years, 4 months, 5 days and 17 hours", but it's not attached to a specific point in time ("10 minutes relative to what?"); it's just the amount of time, by itself.
Although both concepts might use the same words ("hours", "minutes", "seconds", etc), they're not the same thing.
TimeUnit
deals with amounts of time, so it does conversions like "1000 seconds is equivalent to how many minutes?".
Example: TimeUnit.MILLISECONDS.toHours(value)
returns the number of hours equivalent to value
milliseconds (not the time of the day). As the value you're using is the result of getTimeInMillis()
, you're getting the total number of hours since January 1970.
By using the result of getTimeInMillis()
(which represents a specific point in time) with TimeUnit
, you're misusing the value, treating like it was an amount of time.
To format a date, you can use a SimpleDateFormat
:
Calendar mycalendar = Calendar.getInstance();
mycalendar.set(Calendar.HOUR_OF_DAY, 1);
mycalendar.set(Calendar.MINUTE, 30);
mycalendar.set(Calendar.SECOND, 0);
// format: hour:minute:second
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String hms = sdf.format(mycalendar.getTime()); // 01:30:00
With this, hms
will have the value 01:30:00
. Check the javadoc to know what the format HH:mm:ss
means, so you can change it in case you need a different format (If you want just 01:30
, for example, the format will be HH:mm
).
Java new Date/Time API
The old classes (Date
, Calendar
and SimpleDateFormat
) have lots of problems and design issues, and they're being replaced by the new APIs.
In Android you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. To make it work, you'll also need the ThreeTenABP (more on how to use it here).
First you create a org.threeten.bp.LocalTime
(a class that represents a time of the day), then you format it with a org.threeten.bp.format.DateTimeFormatter
:
// create time for 01:30
LocalTime time = LocalTime.of(1, 30);
// format: hour:minute:second
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("HH:mm:ss");
String hms = fmt.format(time);
System.out.println(hms); // 01:30:00
Also, check the javadoc for all available formats.
android convert date string to time in milliseconds
Use MM for month.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
and to get Time use Date.getTime() method.
for more information take a look at Date and Time Patterns
How to convert milliseconds to date in SQLite
One of SQLite's supported date/time formats is Unix timestamps, i.e., seconds since 1970.
To convert milliseconds to that, just divide by 1000.
Then use some date/time function to get the year and the month:
SELECT strftime('%Y-%m', MillisField / 1000, 'unixepoch') FROM MyTable
Related Topics
Why Is String.Chars() a Stream of Ints in Java 8
Calculate Days Between Two Dates in Java 8
Using Java 8's Optional with Stream::Flatmap
Is It Expensive to Use Try-Catch Blocks Even If an Exception Is Never Thrown
Relationship Between Hashcode and Equals Method in Java
How to Change the Classpath Within Java
How to Create a Zip File in Java
Representing Monetary Values in Java
Java 7 (Jdk 7) Garbage Collection and Documentation on G1
How to Convert a Java 8 Intstream to a List
What Is the Size of a Boolean Variable in Java
Strip Leading and Trailing Spaces from Java String
Java: Run a Function After a Specific Number of Seconds
When Exactly Do You Use the Volatile Keyword in Java
Should One Call .Close() on Httpservletresponse.Getoutputstream()/.Getwriter()
How to Write Console Output to a Txt File