package org.kodejava.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 = "25/09/2021";
// Create a SimpleDateFormat which will be used 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 = Sat Sep 25 00:00:00 CST 2021
Date in Long = 1632499200000
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024