You may use java.util.Date class and then use SimpleDateFormat to format the Date.

Date date=new Date(millis);

We can use java.time package (tutorial) - DateTime APIs introduced in the Java SE 8.

var instance = java.time.Instant.ofEpochMilli(millis);
var localDateTime = java.time.LocalDateTime
                        .ofInstant(instance, java.time.ZoneId.of("Asia/Kolkata"));
var zonedDateTime = java.time.ZonedDateTime
                            .ofInstant(instance,java.time.ZoneId.of("Asia/Kolkata"));

// Format the date

var formatter = java.time.format.DateTimeFormatter.ofPattern("u-M-d hh:mm:ss a O");
var string = zonedDateTime.format(formatter);
Answer from KV Prajapati on Stack Overflow
🌐
FileFormat
fileformat.info › tip › java › date2millis.htm
Online conversion between a java.util.Date and milliseconds
This is just simple page that I wrote after having written the 3 line program once too often. The standard date parse is a little finicky: dates should look like 3/23/26, 12:17 PM
🌐
Current Millis
currentmillis.com
Current Millis ‐ Milliseconds since Unix Epoch
local date · UTC time · local time · UNIX time · Convert milliseconds · to UTC time & date: to local time & date: UNIX · J2000 · Contact - Mission - Support - Terms © 2013 - 2018 currentmillis.com · Convert local YYYY / MM / DD · and HH : MM : SS ·
Top answer
1 of 5
140

You don't have a Date, you have a String representation of a date. You should convert the String into a Date and then obtain the milliseconds. To convert a String into a Date and vice versa you should use SimpleDateFormat class.

Here's an example of what you want/need to do (assuming time zone is not involved here):

CopyString myDate = "2014/10/29 18:10:45";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = sdf.parse(myDate);
long millis = date.getTime();

Still, be careful because in Java the milliseconds obtained are the milliseconds between the desired epoch and 1970-01-01 00:00:00.


Using the new Date/Time API available since Java 8:

CopyString myDate = "2014/10/29 18:10:45";
LocalDateTime localDateTime = LocalDateTime.parse(myDate,
    DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss") );
/*
  With this new Date/Time API, when using a date, you need to
  specify the Zone where the date/time will be used. For your case,
  seems that you want/need to use the default zone of your system.
  Check which zone you need to use for specific behaviour e.g.
  CET or America/Lima
*/
long millis = localDateTime
    .atZone(ZoneId.systemDefault())
    .toInstant().toEpochMilli();
2 of 5
10

tl;dr

CopyLocalDateTime.parse(           // Parse into an object representing a date with a time-of-day but without time zone and without offset-from-UTC.
    "2014/10/29 18:10:45"      // Convert input string to comply with standard ISO 8601 format.
    .replace( " " , "T" )      // Replace SPACE in the middle with a `T`.
    .replace( "/" , "-" )      // Replace SLASH in the middle with a `-`.
)
.atZone(                       // Apply a time zone to provide the context needed to determine an actual moment.
    ZoneId.of( "Europe/Oslo" ) // Specify the time zone you are certain was intended for that input.
)                              // Returns a `ZonedDateTime` object.
.toInstant()                   // Adjust into UTC.
.toEpochMilli()                // Get the number of milliseconds since first moment of 1970 in UTC, 1970-01-01T00:00Z.

1414602645000

Time Zone

The accepted answer is correct, except that it ignores the crucial issue of time zone. Is your input string 6:10 PM in Paris or Montréal? Or UTC?

Use a proper time zone name. Usually a continent plus city/region. For example, "Europe/Oslo". Avoid the 3 or 4 letter codes which are neither standardized nor unique.

java.time

The modern approach uses the java.time classes.

Alter your input to conform with the ISO 8601 standard. Replace the SPACE in the middle with a T. And replace the slash characters with hyphens. The java.time classes use these standard formats by default when parsing/generating strings. So no need to specify a formatting pattern.

CopyString input = "2014/10/29 18:10:45".replace( " " , "T" ).replace( "/" , "-" ) ;
LocalDateTime ldt = LocalDateTime.parse( input ) ;

A LocalDateTime, like your input string, lacks any concept of time zone or offset-from-UTC. Without the context of a zone/offset, a LocalDateTime has no real meaning. Is it 6:10 PM in India, Europe, or Canada? Each of those places experience 6:10 PM at different moments, at different points on the timeline. So you must specify which you have in mind if you want to determine a specific point on the timeline.

CopyZoneId z = ZoneId.of( "Europe/Oslo" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;  

Now we have a specific moment, in that ZonedDateTime. Convert to UTC by extracting a Instant. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

CopyInstant instant = zdt.toInstant() ;

Now we can get your desired count of milliseconds since the epoch reference of first moment of 1970 in UTC, 1970-01-01T00:00Z.

Copylong millisSinceEpoch = instant.toEpochMilli() ; 

Be aware of possible data loss. The Instant object is capable of carrying microseconds or nanoseconds, finer than milliseconds. That finer fractional part of a second will be ignored when getting a count of milliseconds.


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.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

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.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.


Joda-Time

Update: The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. I will leave this section intact for history.

Below is the same kind of code but using the Joda-Time 2.5 library and handling time zone.

The java.util.Date, .Calendar, and .SimpleDateFormat classes are notoriously troublesome, confusing, and flawed. Avoid them. Use either Joda-Time or the java.time package (inspired by Joda-Time) built into Java 8.

ISO 8601

Your string is almost in ISO 8601 format. The slashes need to be hyphens and the SPACE in middle should be replaced with a T. If we tweak that, then the resulting string can be fed directly into constructor without bothering to specify a formatter. Joda-Time uses ISO 8701 formats as it's defaults for parsing and generating strings.

Example Code

CopyString inputRaw = "2014/10/29 18:10:45";
String input = inputRaw.replace( "/", "-" ).replace( " ", "T" );
DateTimeZone zone = DateTimeZone.forID( "Europe/Oslo" ); // Or DateTimeZone.UTC
DateTime dateTime = new DateTime( input, zone );
long millisecondsSinceUnixEpoch = dateTime.getMillis();
🌐
Baeldung
baeldung.com › home › java › java dates › convert time to milliseconds in java
Convert Time to Milliseconds in Java | Baeldung
May 2, 2019 - First, we created an instance of the current date. After that, we used the toEpochMilli() method to convert the ZonedDateTime into milliseconds. As we know, LocalDateTime doesn’t contain information about the time zone.
🌐
BeginnersBook
beginnersbook.com › 2014 › 01 › how-to-get-time-in-milliseconds-in-java
Java – Get time in milliseconds using Date, Calendar and ZonedDateTime
September 11, 2022 - import java.util.Calendar; import ... Date(); //This method returns the time in millis long timeMilli = date.getTime(); System.out.println("Time in milliseconds using Date class: " + timeMilli); //creating Calendar instance Calendar calendar = Calendar.getInstance(); //Returns ...
🌐
Blogger
javarevisited.blogspot.com › 2012 › 12 › how-to-convert-millisecond-to-date-in-java-example.html
How to convert milliseconds to Date in Java - Tutorial example
Actually java.util.Date is internally specified in milliseconds from epoch. So any date is the number of milliseconds passed since January 1, 1970, 00:00:00 GMT and Date provides constructor which can be used to create Date from milliseconds.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › java-program-to-convert-date-into-milliseconds
Java Program to convert Date into milliseconds
June 27, 2020 - ... import java.util.Date; public class Demo { public static void main(String[] args) { Date d = new Date(); System.out.println("Date = " + d); System.out.println("Milliseconds since January 1, 1970, 00:00:00 GMT = " + d.getTime()); } }
🌐
Simplesolution
simplesolution.dev › java-convert-date-to-milliseconds
Java Convert Date to Milliseconds - Simple Solution
import java.util.Date; public class ... Convert Date to milliseconds long milliseconds = date.getTime(); System.out.println("Date: " + date); System.out.println("Milliseconds: " + milliseconds); } } The output as belo...
🌐
The Roosevelt Review
therooseveltreview.com › date-to-milliseconds-java
Date to milliseconds java - The Roosevelt Review
July 20, 2024 - Indicate whether any date class ... java gettime on the instant in the class date class, divide seconds. One can use java 8 object into milliseconds to convert the string is a method gettime method....
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java - Get Time In MilliSeconds - Java Code Geeks
December 17, 2020 - Date class has a method getTime() which returns the milliseconds in long value for the given time or current time. package com.javaprogramto.java8.dates.milliseconds; import java.util.Date; /** * Example to get time in milli seconds in java ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-get-time-in-milliseconds-for-the-given-date-and-time-in-java
How to get Time in Milliseconds for the Given date and time in Java?
February 7, 2022 - You can set date and time values to a calendar object using the set() method. The getTimeInMillis() of this class returns the epoch time of the date value. ... import java.util.Calendar; public class Sample { public static void main(String args[]) { Calendar cal = Calendar.getInstance(); ...
🌐
Westwaytowing
westwaytowing.com › date-to-milliseconds-java
Date to milliseconds java – WestWay Towing
What is a java. Currenttimemillis method and milliseconds from a thin wrapper around a milliseconds in milliseconds from a static method. Here's an example, 1970, we'll also discuss possible use these. Calendar class that already. Let us convert milliseconds value represents the value is used ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › program-to-convert-milliseconds-to-a-date-format-in-java
Program to Convert Milliseconds to a Date Format in Java - GeeksforGeeks
July 11, 2025 - // Java program to convert milliseconds ... MMM yyyy HH:mm:ss:SSS Z"); // Creating date from milliseconds // using Date() constructor Date result = new Date(milliSec); // Formatting Date according to the // given format System.out.pr...
🌐
Coderanch
coderanch.com › t › 635950 › java › Milliseconds-Date-Calendar-Epoch
Milliseconds to Date using Calendar [without using Epoch] (Java in General forum at Coderanch)
July 1, 2014 - The date is probably 21:00:01 on December 31st 1969. That indicates there's indeed a timezone issue. If I run your code I get 001:01:00:01,000. Changing the time zone of the Calendar does not help. However, you can also change the time zone of your SimpleDateFormat. If I do that I get 001:12:00:01,000 (or 001:00:00:01,000 if I switch to HH).
🌐
Javaprogramto
javaprogramto.com › 2020 › 12 › java-get-current-time-in-milliseconds.html
Java - Get Time In MilliSeconds JavaProgramTo.com
May 7, 2024 - Date class has a method getTime() which returns the milliseconds in long value for the given time or current time. package com.javaprogramto.java8.dates.milliseconds; import java.util.Date; /** * Example to get time in milli seconds in java ...
🌐
Coderanch
coderanch.com › t › 523155 › java › Date-milliseconds
Date milliseconds [Solved] (Java in General forum at Coderanch)
programming forums Java Mobile ... by our volunteer staff, including ... ... long time = System.currentTimeMillis(); SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss:ms"); java.util.Date resultdate = new java.util.Date(time); //java.util.Date resultdate1 = ...
🌐
Tutorjoes
tutorjoes.in › Java_example_programs › get_milliseconds_from_the_specified_date_in_java
Write a Java Program to Get milliseconds from the specified date
January 22, 2015 - Date class to get the milliseconds of the date and print it to the console. Catch any exception that may occur during the execution of the program using a · catch block. import java.util.*; import java.text.SimpleDateFormat; public class Millisecond_SpecifiedDate { public static void main(String[] args) { SimpleDateFormat dt_formate = new SimpleDateFormat("dd-M-yyyy"); String str_date = "18-06-2014"; try { Date dt = dt_formate.parse(str_date); System.out.println("Date in Milliseconds : " + dt.getTime()); } catch (Exception e) { e.printStackTrace(); } } } Date in Milliseconds : 1403029800000 ·
🌐
Mkyong
mkyong.com › home › java › how to get time in milliseconds in java
How to get time in milliseconds in Java - Mkyong.com
January 22, 2015 - package com.mkyong.test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class TimeMilisecond { public static void main(String[] argv) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss"); String dateInString = "22-01-2015 10:20:56"; Date date = sdf.parse(dateInString); System.out.println(dateInString); System.out.println("Date - Time in milliseconds : " + date.getTime()); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); System.out.println("Calender - Time in milliseconds : " + calendar.getTimeInMillis()); } }