diff --git a/java-dates/src/main/java/com/baeldung/convert/ConvertDateTime.java b/java-dates/src/main/java/com/baeldung/convert/ConvertDateTime.java index 548912ea07..c4d771a7e6 100644 --- a/java-dates/src/main/java/com/baeldung/convert/ConvertDateTime.java +++ b/java-dates/src/main/java/com/baeldung/convert/ConvertDateTime.java @@ -4,16 +4,39 @@ import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; +import java.util.Calendar; +import java.util.Date; public class ConvertDateTime { - public static void main(String[] args) { + public static void main(String[] args) throws ParseException { java8(); joda(); + Date date = simpleDateTimeFormatter(); + calendar(date); + } + + private static void calendar(Date date) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + System.out.println("Calender - Time in milliseconds : " + calendar.getTimeInMillis()); + } + + private static Date simpleDateTimeFormatter() 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()); + + return date; } private static void joda() {