在Java中,處理日期和時間有很多方法,主要涉及到以下幾個類:java.util.Date
、java.util.Calendar
、java.text.SimpleDateFormat
和 java.time
包中的類(如 LocalDate
、LocalTime
、LocalDateTime
等)。
以下是一些常見的Java指令,用于處理日期和時間:
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date currentDate = new Date();
System.out.println("當前日期和時間: " + currentDate);
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date currentDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(currentDate);
System.out.println("格式化后的日期: " + formattedDate);
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
String dateString = "2022-01-01 12:00:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date parsedDate = sdf.parse(dateString);
System.out.println("解析后的日期: " + parsedDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date currentDate = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1; // 月份從0開始
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("年份: " + year + ", 月份: " + month + ", 日期: " + day);
}
}
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date1 = new Date();
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date1);
calendar1.add(Calendar.DAY_OF_MONTH, 7);
Date date2 = calendar1.getTime();
System.out.println("日期1: " + date1);
System.out.println("日期2: " + date2);
System.out.println("日期差: " + (date2.getTime() - date1.getTime()));
}
}
從Java 8開始,推薦使用 java.time
包中的類來處理日期和時間,因為它們提供了更強大和易用的功能。以下是一些使用 java.time
包的示例:
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
System.out.println("當前日期: " + currentDate);
}
}
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = currentDate.format(formatter);
System.out.println("格式化后的日期: " + formattedDate);
}
}
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String dateString = "2022-01-01";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate parsedDate = LocalDate.parse(dateString, formatter);
System.out.println("解析后的日期: " + parsedDate);
}
}
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
int year = currentDate.getYear();
int month = currentDate.getMonthValue();
int day = currentDate.getDayOfMonth();
System.out.println("年份: " + year + ", 月份: " + month + ", 日期: " + day);
}
}
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class Main {
public static void main(String[] args) {
LocalDate date1 = LocalDate.now();
LocalDate date2 = date1.plusDays(7);
System.out.println("日期1: " + date1);
System.out.println("日期2: " + date2);
System.out.println("日期差: " + ChronoUnit.DAYS.between(date1, date2));
}
}