Skip to content

Commit a362ace

Browse files
committed
feat(datetime-api): add DateDemo showcasing legacy Date vs java.time API
What - Introduced DateDemo under DateandTimeAPI package. - Demonstrates usage of both legacy java.util.Date and modern java.time API. - Key operations shown: - Printing current date/time using Date. - Modifying time via setHours() (legacy, deprecated). - Creating LocalDate with different factories: - now() with system default clock. - now(ZoneId) for specific time zones. - of(year, month, day) for explicit date. - ofEpochDay() using epoch days. - parse("yyyy-MM-dd") for ISO-8601 parsing. - Performing date arithmetic with plusMonths(). - Using LocalTime and LocalDateTime to retrieve and manipulate times. - Accessing LocalDateTime components (nano, day of month). Why - Highlights contrast between mutable, deprecated java.util.Date and immutable, thread-safe java.time classes. - Provides hands-on examples for creating, parsing, and manipulating dates and times. - Reinforces why java.time is preferred in modern applications. How to use - Run DateDemo. - Output will display: 1. Current date/time using legacy Date. 2. Modified Date after setHours(). 3. Current LocalDate in different ways (default, zone-based, explicit). 4. Dates created from epoch days and parsing. 5. LocalTime with subtraction of hours. 6. LocalDateTime with nano precision and day-of-month extraction. Real-life applications - Parsing user-input dates in forms (LocalDate.parse). - Scheduling events in specific time zones (LocalDate.now(ZoneId)). - Handling recurring billing cycles or reminders (plusMonths()). - Capturing precise timestamps for logging/auditing (LocalDateTime.now()). - Migrating legacy Date-based code to modern java.time API. Notes - java.util.Date is mutable and has deprecated methods (`setHours()`). - LocalDate, LocalTime, LocalDateTime are immutable and safe for concurrency. - java.time supports time zones via ZoneId and ZonedDateTime. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 57c345d commit a362ace

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

Section26DateandTimeAPI/src/JodaTimeAPI/DateDemo.java renamed to Section26DateandTimeAPI/Date/src/DateDemo.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
package JodaTimeAPI;
2-
31
import java.time.*;
42
import java.util.Date;
5-
import java.util.Locale;
63

74
public class DateDemo {
85
public static void main(String[] args) {
96
Date d = new Date();
10-
System.out.println("Todays Date and Time: "+d);
7+
8+
System.out.println("Today's Date and Time: "+d);
119

1210
//d.setTime(21);
1311
d.setHours(21);
12+
1413
System.out.println(d);
1514

16-
//LocalDate dt = new LocalDate(); //you can not create a new date.
15+
//LocalDate dt = new LocalDate();
16+
//you can not create a new date.
1717

1818
LocalDate dt = LocalDate.now();
1919
System.out.println(dt);
2020

21-
LocalDate d2 = LocalDate.now(Clock.systemDefaultZone()); //new date not able to create . Now means today date.
21+
LocalDate d2 = LocalDate.now(Clock.systemDefaultZone());
22+
//the new date is not able to create. Now means date today.
2223
System.out.println(d2);
2324

2425
LocalDate d3 = LocalDate.now(ZoneId.of("Asia/Kolkata"));
@@ -50,4 +51,4 @@ public static void main(String[] args) {
5051
System.out.println(dt1.getNano());
5152
System.out.println(dt1.getDayOfMonth());
5253
}
53-
}
54+
}

0 commit comments

Comments
 (0)