
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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 Articles
- How to get current date/time in milli seconds in Java?
- How to get current date/time in seconds in JavaScript?
- Get current date/time in seconds - JavaScript?
- How to format date and time in android?
- How to get the current date and time in Java?
- Java Program to format date time with Join
- How to get current date and time from internet in iOS?
- How to get current date and time from internet in android?
- Format Date and Time in Perl
- How to convert time seconds to h:m:s format in Python?
- How to get Time in Milliseconds for the Given date and time in Java?
- How to change date and time format in Android sqlite?
- How to change date time format in HTML5?
- How to get the date and time in JShell in Java 9?
- Get Date and Time parts separately in Java

Advertisements