How to get year, month, day, hours, minutes, seconds and milliseconds of Date in Java?

Write a Java program that extract year, month, day, hours, minutes, seconds, and milliseconds of Date in Java.
Check out Java 8 examples at Java 8 Examples

Get Year, Month, Day, Hours, Minutes, Seconds and Milliseconds of Date in Java

package net.sourcecodeexamples.java;

import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.util.Calendar;
import java.util.Date;

public class Main {

    public static void main(String[] args) {

        System.out.println("Before JDK 8:");

        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        int yearC = calendar.get(Calendar.YEAR);
        int monthC = calendar.get(Calendar.MONTH);
        int weekC = calendar.get(Calendar.WEEK_OF_MONTH);
        int dayC = calendar.get(Calendar.DATE);
        int hourC = calendar.get(Calendar.HOUR_OF_DAY);
        int minuteC = calendar.get(Calendar.MINUTE);
        int secondC = calendar.get(Calendar.SECOND);
        int millisC = calendar.get(Calendar.MILLISECOND);

        System.out.println("Date: " + date);
        System.out.println("Year: " + yearC + " Month: " + monthC + " Week: " + weekC + " Day: " + dayC + " Hour: " +
            hourC + " Minute: " + minuteC + " Second: " + secondC + " Millis: " + millisC);

        System.out.println("\nStarting with JDK 8:");

        LocalDateTime ldt = LocalDateTime.now();

        int yearLDT = ldt.getYear();
        int monthLDT = ldt.getMonthValue();
        int dayLDT = ldt.getDayOfMonth();
        int hourLDT = ldt.getHour();
        int minuteLDT = ldt.getMinute();
        int secondLDT = ldt.getSecond();
        int nanoLDT = ldt.getNano();

        System.out.println("LocalDateTime: " + ldt);
        System.out.println("Year: " + yearLDT + " Month: " + monthLDT + " Day: " + dayLDT + " Hour: " + hourLDT +
            " Minute: " + minuteLDT + " Second: " + secondLDT + " Nano: " + nanoLDT);

        int yearLDT2 = ldt.get(ChronoField.YEAR);
        int monthLDT2 = ldt.get(ChronoField.MONTH_OF_YEAR);
        int dayLDT2 = ldt.get(ChronoField.DAY_OF_MONTH);
        int hourLDT2 = ldt.get(ChronoField.HOUR_OF_DAY);
        int minuteLDT2 = ldt.get(ChronoField.MINUTE_OF_HOUR);
        int secondLDT2 = ldt.get(ChronoField.SECOND_OF_MINUTE);
        int nanoLDT2 = ldt.get(ChronoField.NANO_OF_SECOND);

        System.out.println("Year: " + yearLDT2 + " Month: " + monthLDT2 + " Day: " + dayLDT2 + " Hour: " + hourLDT2 +
            " Minute: " + minuteLDT2 + " Second: " + secondLDT2 + " Nano: " + nanoLDT2);
    }

}

Output

Before JDK 8:
Date: Tue Aug 11 14:46:26 IST 2020
Year: 2020 Month: 7 Week: 3 Day: 11 Hour: 14 Minute: 46 Second: 26 Millis: 981

Starting with JDK 8:
LocalDateTime: 2020-08-11T14:46:27.087
Year: 2020 Month: 8 Day: 11 Hour: 14 Minute: 46 Second: 27 Nano: 87000000
Year: 2020 Month: 8 Day: 11 Hour: 14 Minute: 46 Second: 27 Nano: 87000000

Related Posts


Comments