

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get (format) date and time from mill seconds in Java?
The java.text.SimpleDateFormat class is used to format and parse a string to date and date to string.
One of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat object.
To format milli seconds to date −
- Create the format string as dd MMM yyyy HH:mm:ss:SSS Z.
- The Date class constructor accepts a long value representing the milliseconds as a parameter and creates a date object.
- Finally format the date object using the format() method.
Example
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Sample { public static void main(String args[]) throws ParseException { long msec = 3550154; //Instantiating the SimpleDateFormat class SimpleDateFormat dateformatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss:SSS Z"); //Parsing the given String to Date object Date date = new Date(msec); System.out.println("Date value: "+dateformatter.format(date)); } }
Output
Date value: 01 Jan 1970 06:29:10:154 +0530
Example
import java.util.Calendar; import java.util.Date; public class Sample { public static void main(String args[]) { Calendar calendar = Calendar.getInstance(); long msec = calendar.getTimeInMillis(); Date obj = new Date(msec); System.out.println(obj); } }
Output
Wed Nov 11 22:04:59 IST 2020
- Related Questions & Answers
- How to get current date/time in milli seconds in Java?
- Get current date/time in seconds - JavaScript?
- How to get current date/time in seconds in JavaScript?
- How to format date and time in android?
- Format Date and Time in Perl
- How to get the current date and time in Java?
- How to convert time seconds to h:m:s format in Python?
- How to get current date and time from internet in android?
- How to get current date and time from internet in iOS?
- Java Program to format date time with Join
- How to change date and time format in Android sqlite?
- Format Seconds in s format in Java
- Get Date and Time parts separately in Java
- How to get Time in Milliseconds for the Given date and time in Java?
- How to change date time format in HTML5?
Advertisements