杰瑞科技汇

string calendar java

Of course! In Java, working with dates and times has evolved significantly over the years. I'll guide you through the three main eras, from the classic but flawed java.util.Date to the modern and powerful java.time package.

string calendar java-图1
(图片来源网络,侵删)

The Classic (and Flawed) java.util.Date & java.text.SimpleDateFormat

Before Java 8, this was the standard way to handle dates. It's important to know it because you'll still encounter it in older codebases, but you should avoid using it in new code.

Key Problems with java.util.Date:

  • Misnamed: It represents an instant in time (a timestamp), not just a date.
  • Poor API: Months are 0-indexed (January is 0), which is a common source of bugs.
  • Not Thread-Safe: SimpleDateFormat is notoriously not thread-safe, leading to errors if used in a multi-threaded environment without synchronization.
  • Confusing: Mixing date and time components makes it clumsy to work with just a date or just a time.

Example: java.util.Date

import java.util.Date;
public class LegacyDateExample {
    public static void main(String[] args) {
        // Represents the current date and time
        Date now = new Date();
        System.out.println("Current Date & Time (raw): " + now);
        // Note: Months are 0-indexed (0=Jan, 1=Feb, ...)
        Date specificDate = new Date(2025, 10, 25); // Nov 25, 2025
        System.out.println("Specific Date (raw): " + specificDate);
    }
}

Example: java.text.SimpleDateFormat for Formatting

This is how you would convert a Date object to a String.

import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatExample {
    public static void main(String[] args) {
        Date now = new Date();
        System.out.println("Raw Date: " + now);
        // Create a formatter. NOT THREAD-SAFE!
        // Common pattern symbols: yyyy=year, MM=month, dd=day, HH=hour, mm=minute, ss=second
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // Format the date into a string
        String formattedDateTime = formatter.format(now);
        System.out.println("Formatted Date: " + formattedDateTime);
        // You can also parse a string back into a Date
        String dateString = "2025-12-31 23:59:59";
        try {
            Date parsedDate = formatter.parse(dateString);
            System.out.println("Parsed Date: " + parsedDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The Modern Standard: java.time (Java 8+)

This is the recommended, modern, and correct way to handle dates and times in Java. It was introduced in Java 8 to fix all the flaws of the old Date/Calendar classes.

The java.time package is based on the ISO-8601 standard and is immutable, thread-safe, and has a much richer API.

string calendar java-图2
(图片来源网络,侵删)

Key Classes in java.time:

  • LocalDate: Represents a date (year, month, day) without a time. e.g., 2025-11-25
  • LocalTime: Represents a time (hour, minute, second, nanosecond) without a date. e.g., 10:15:30
  • LocalDateTime: Represents a date and time without a time zone. e.g., 2025-11-25T10:15:30
  • ZonedDateTime: Represents a date and time with a time zone. e.g., 2025-11-25T10:15:30+01:00[Europe/Paris]
  • DateTimeFormatter: The modern replacement for SimpleDateFormat for formatting and parsing.

Practical Examples with java.time

Getting the Current Date and Time

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.ZoneId;
public class CurrentDateTimeExample {
    public static void main(String[] args) {
        // Current date (without time)
        LocalDate today = LocalDate.now();
        System.of("Current Date: " + today).println();
        // Current time (without date)
        LocalTime now = LocalTime.now();
        System.of("Current Time: " + now).println();
        // Current date and time (without time zone)
        LocalDateTime nowDateTime = LocalDateTime.now();
        System.of("Current Date & Time: " + nowDateTime).println();
        // Current date and time with a specific time zone
        ZonedDateTime nowInParis = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
        System.of("Current Date & Time in Paris: " + nowInParis).println();
    }
}

Creating a Specific Date or String to Date Conversion

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class CreateAndParseDate {
    public static void main(String[] args) {
        // --- Creating a specific date ---
        LocalDate independenceDay = LocalDate.of(1776, 7, 4);
        System.of("Independence Day: " + independenceDay).println();
        // --- Parsing a String into a LocalDate ---
        String dateString = "2025-12-25";
        LocalDate christmas = LocalDate.parse(dateString);
        System.of("Christmas (from string): " + christmas).println();
        // --- Parsing with a custom format ---
        String formattedDateString = "25-Dec-2025";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
        LocalDate anotherDate = LocalDate.parse(formattedDateString, formatter);
        System.of("Another Date (custom format): " + anotherDate).println();
    }
}

Formatting a Date to a String

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class FormatDateExample {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        // --- Using predefined formatters ---
        String isoFormat = today.format(DateTimeFormatter.ISO_DATE);
        System.of("ISO Format: " + isoFormat).println();
        // --- Using a custom pattern ---
        DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy");
        String prettyFormat = today.format(customFormatter);
        System.of("Pretty Format: " + prettyFormat).println(); // e.g., "Saturday, November 25, 2025"
    }
}

Manipulating Dates (Adding/Subtracting Time)

Since java.time objects are immutable, methods like plusDays() or minusWeeks() return a new object instead of modifying the original.

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class DateManipulationExample {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        System.of("Today: " + today).println();
        // --- Adding time ---
        LocalDate nextWeek = today.plusWeeks(1);
        LocalDate nextMonth = today.plusMonths(1);
        LocalDate nextYear = today.plusYears(1);
        System.of("Next Week: " + nextWeek).println();
        System.of("Next Month: " + nextMonth).println();
        System.of("Next Year: " + nextYear).println();
        // --- Subtracting time ---
        LocalDate lastWeek = today.minus(1, ChronoUnit.WEEKS);
        LocalDate yesterday = today.minusDays(1);
        System.of("Last Week: " + lastWeek).println();
        System.of("Yesterday: " + yesterday).println();
    }
}

Calculating the Difference Between Dates

The ChronoUnit enum is perfect for calculating the difference between two temporal objects.

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class DateDifferenceExample {
    public static void main(String[] args) {
        LocalDate startDate = LocalDate.of(2025, 1, 1);
        LocalDate endDate = LocalDate.of(2025, 1, 1);
        long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
        long weeksBetween = ChronoUnit.WEEKS.between(startDate, endDate);
        long yearsBetween = ChronoUnit.YEARS.between(startDate, endDate);
        System.of("Days between: " + daysBetween).println(); // 365 or 366 in a leap year
        System.of("Weeks between: " + weeksBetween).println();
        System.of("Years between: " + yearsBetween).println(); // 1
    }
}

Legacy Bridge: java.util.Calendar

This class was introduced as a partial fix for java.util.Date but is also flawed and part of the old API. You should avoid it in new code. It's mainly used for legacy compatibility.

import java.util.Calendar;
import java.util.Date;
public class CalendarExample {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance(); // Gets current date and time
        // Months are STILL 0-indexed!
string calendar java-图3
(图片来源网络,侵删)
分享:
扫描分享到社交APP
上一篇
下一篇