Loading topic
java.util.Date and java.util.Calendar. Those classes caused so many bugs that Java 8 added a new package, java.time, to replace them.5 min readjava.time follows one rule. Once you create a date or time object, it never changes. This chapter explains what that rule means, why it protects your programs, and the one mistake that almost every beginner makes because of it.4 min readjava.time, the type for these is LocalDate.4 min readLocalTime is a time of day with no date and no time zone. It holds an hour, a minute, a second, and a fraction of a second. "7:15 in the morning" and "half past six in the evening" are good examples of times it can hold.4 min readLocalDate holds a calendar date. A LocalTime holds a time of day. Many real tasks need both at once: a meeting on 1 July at 9:30, or a shop that opens on Saturday at 10:00. LocalDateTime covers these cases. It is a class that holds a date and a time together in one object.4 min readLocalDate, LocalTime, and LocalDateTime. These types describe what a calendar or a wall clock shows. They do not say where in the world that calendar or clock is. So "2024-07-01 09:00" happens at different moments in New York and in Tokyo.4 min readLocalDate and LocalDateTime. They have no idea where in the world they are. "9:00 on July 1" happens at a different moment in New York than it does in Tokyo. You need a time zone to turn a local time into a real moment.4 min readLocalDateTime says "9:00 on July 1" but does not say where. A meeting at 9:00 in New York and a meeting at 9:00 in Tokyo happen at different moments. To pin a date and time to one exact moment, you need a time zone. In this chapter you will join a date-time to a zone, read its parts, and convert4 min readZonedDateTime to show a meeting time in New York and in Tokyo. That type works well when a program needs to follow a region's clock rules. When you save a time to a database or send it to another program, you usually want something simpler that has the same meaning eve4 min readDuration is an amount of time measured in hours, minutes, seconds, and nanoseconds. It answers questions like "How long did this task take?" or "How long is this work shift?" A Duration is not tied to any date or clock. It is only a length of time, such as "2 hours and 15 minutes."4 min readPeriod class for this. A Period is an amount of time stored as years, months, and days. It is date-based, so it works with LocalDate and other types th4 min readPeriod gives you a mix of years, months, and days, and Duration gives you hours, minutes, and seconds. Sometimes you want a single number instead. How many days until a deadline? How many minutes did a task take? ChronoUnit answers those questions.4 min readplusDays and a loop. Java gives you a shorter and safer way: TemporalAdjusters.4 min readjava.time handles each cas4 min readjava.util.Date, java.util.Calendar, and the java.sql date classes. This chapter shows how to move values between those legacy types and java.time. A legacy type is an older class that is sti4 min read