package org.kodejava.example.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringDateToLong {
public static void main(String[] args) {
// Here we have a string date and we want to covert it to long value
String today = "13/10/2017";
// Create a SimpleDateFormat which will be use to convert the string to
// a date object.
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
try {
// The SimpleDateFormat parse the string and return a date object.
// To get the date in long value just call the getTime method of
// the Date object.
Date date = formatter.parse(today);
long dateInLong = date.getTime();
System.out.println("Date = " + date);
System.out.println("Date in Long = " + dateInLong);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
The result of the code snippet:
Date = Fri Oct 13 00:00:00 WITA 2017
Date in Long = 1507824000000
Wayan Saryada
Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. If these posts help, you can support me, buy me a cup of coffee or tea. Thank you 🥳
Latest posts by Wayan Saryada (see all)
- How do I set the time of java.util.Date instance to 00:00:00? - October 24, 2019
- How to Install Consolas Font in Mac OS X? - March 29, 2019
- How do I clear the current command line in terminal? - February 14, 2019