Simple java.time.LocalDate examples

Add following imports:


import java.time.LocalDate;

import java.time.ZoneId;

import java.time.format.DateTimeFormatter;


Following are examples for displaying date:


// Get current LocalDate
LocalDate date = LocalDate.now(); // e.g., 28 April 2025

// Format LocalDate
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMMM yyyy");
String formattedDate = date.format(formatter); // "28 April 2025"

// Display LocalDate
textview2.setText(date.toString()); 
// Result: "2025-04-28" (ISO format)

textview3.setText(formattedDate); 
// Result: "28 April 2025"

// LocalDate of a specific time zone
ZoneId zone1 = ZoneId.of("Asia/Kolkata"); // or ZoneId.of("+05:30")
LocalDate date2 = LocalDate.now(zone1);

textview4.setText(date2.format(formatter)); 
// Result: "28 April 2025" (same date if system time zone matches, otherwise adjusted)

// LocalDate from year, month, and day of month
LocalDate date3 = LocalDate.of(2025, 4, 24);
textview5.setText(date3.format(formatter)); 
// Result: "24 April 2025"

// LocalDate from year and day of year
LocalDate date4 = LocalDate.ofYearDay(2025, 114);
textview6.setText(date4.format(formatter)); 
// Result: "24 April 2025" (114th day of 2025 is 24 April)

// LocalDate from String (ISO format)
LocalDate date5 = LocalDate.parse("2025-04-24");
textview7.setText(date5.format(formatter)); 
// Result: "24 April 2025"

// LocalDate from String (custom format)
LocalDate date6 = LocalDate.parse("25 April 2025", DateTimeFormatter.ofPattern("dd MMMM yyyy"));
textview8.setText(date6.format(formatter)); 
// Result: "25 April 2025"

Comments

Popular posts from this blog

Simple car racing android game in Sketchware

How to enable upload from webview in Sketchware?

Simple Audio recorder app in Sketchware

Custom dialog box in Sketchware using CustomView

Retrieve contact list in Sketchware