Master Java Dates & Times in 8 Minutes ๐Ÿ“…

Quickly learn Java's date and time classes like Instant, LocalDate, and LocalTime in just 8 minutes!

Master Java Dates & Times in 8 Minutes ๐Ÿ“…
Bro Code
20.8K views โ€ข Dec 17, 2024
Master Java Dates & Times in 8 Minutes ๐Ÿ“…

About this video

#java #javatutorial #javacourse

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Main {
public static void main(String[] args) {

// How to work with DATES & TIMES using Java
// (LocalDate, LocalTime, LocalDateTime, UTC timestamp)

// Current date
LocalDate date = LocalDate.now();
System.out.println(date);

// Current time
LocalTime time = LocalTime.now();
System.out.println(time);

// Current date and time
LocalDateTime dateTime = LocalDateTime.now();
System.out.println(dateTime);

// UTC timestamp
Instant instant = Instant.now();
System.out.println(instant);

// Custom format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String newDateTime = dateTime.format(formatter);
System.out.println(newDateTime);

// Comparing dates & times
LocalDateTime date1 = LocalDateTime.of(2024, 12, 25, 12, 0, 0); // CHRISTMAS
LocalDateTime date2 = LocalDateTime.of(2025, 1, 1, 0, 0, 0); // NEW YEARS DAY

if(date1.isBefore(date2)){
System.out.println(date1 + " is earlier than " + date2);
}
else if(date1.isAfter(date2)){
System.out.println(date1 + " is later than " + date2);
}
else if(date1.isEqual(date2)){
System.out.println(date1 + " is equal to " + date2);
}
}
}

Tags and Topics

Browse our collection to discover more content in these categories.

Video Information

Views

20.8K

Likes

514

Duration

8:44

Published

Dec 17, 2024

User Reviews

4.6
(4)
Rate:

Related Trending Topics

LIVE TRENDS

Related trending topics. Click any trend to explore more videos.