Examples
LocalDate
1. Extract day of week
Given input is year, monthe and day. Find the day of week.
public String getDay(int day, int month, int year) {
LocalDate localDate = LocalDate.of(year, month, day);
return localDate.getDayOfWeek().name(); // Example: WEDNESDAY
}LocalDateTime
1. Given a date and time in string. Attach a timezone to it.
LocalDateTime date = LocalDateTime.parse("2023-09-11T11:40");This LocalDateTime has no timezone information — it just means "11:40 on Sept 11th, 2023", but not in any particular zone.
// System default: UTC
ZonedDateTime utcZoned = date.atZone(ZoneId.of("UTC"));
// KSA: Asia/Riyadh (UTC+3)
ZonedDateTime ksaZoned = date.atZone(ZoneId.of("Asia/Riyadh"));
System.out.println("UTC Zoned: " + utcZoned.toInstant()); // UTC Zoned: 2023-09-11T11:40:00Z
System.out.println("KSA Zoned: " + ksaZoned.toInstant()); // KSA Zoned: 2023-09-11T08:40:00Z
// Convert to Date
Date ksaDate = Date.from(ksaZoned.toInstant());
Date utcDate = Date.from(utcZoned.toInstant());ZonedDateTime
1. Get current UTC time in yyyy-MM-dd'T'HH:mm:ss.SSSZ format
2. Convert the current time to Saudi Arabia time, which is in the Asia/Riyadh time zone, and yyyy-MM-dd'T'HH:mm:ss.SSSZ format
Asia/Riyadh time zone, and yyyy-MM-dd'T'HH:mm:ss.SSSZ format3. Get a datetime in "dd/MM/yyyy HH:mm" with ZoneId ZONEID_ASIA_RIYADH = ZoneId.of("Asia/Riyadh")
OffsetDateTime
1. Convert a given String OffsetDateTime to different Timezone
2. OffsetDateTime to LocalDate
3. OffsetDateTime to ZonedDateTime
4. OffsetDateTime to String
Comparison
In java, I have below samples file with pattern <string>-DDMMYYYY_HHmmss.csv
Write a code to compare timestamp and select latest file
Last updated