跳到主要内容

日期与日历处理

Date

package com.data_calendar_learning;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {
public static void main(String[] args) {
Date currentDate = new Date();
// 输出实例(标准时间) Tue Oct 10 14:04:46 CST 2023
System.out.println(currentDate);
// 输出毫秒数 (1970.1.1 起到现在的毫秒数)
// 毫秒数 1696917968558 13位 秒数为10位
System.out.println(currentDate.getTime());
// 毫秒转标准时间
Date parsedDate = new Date(1696917968558L);
System.out.println(parsedDate);
// 日期格式化
Date formattedDate = new Date();
// yyyy-MM-dd HH:mm 格式与大小写一定要正确
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
SimpleDateFormat formatterChinese = new SimpleDateFormat("yyyy年MM月dd日");
String dateString = formatter.format(formattedDate);
// 2023-10-10 14:15
System.out.println(dateString);
String dateStringChinese = formatterChinese.format(formattedDate);
System.out.println(dateStringChinese);
}
}

Calendar

类的设计遵循了工厂方法模式,而不是直接允许通过构造函数来创建实例。

package com.data_calendar_learning;

import java.util.Calendar;

public class Test1 {
public static void main(String[] args) {
Calendar calendarInstance = Calendar.getInstance();
// Tue Oct 10 14:25:40 CST 2023
System.out.println(calendarInstance.getTime());
// 设置日历的年月日
calendarInstance.set(Calendar.DAY_OF_MONTH, 1);
System.out.println(calendarInstance.get(Calendar.DAY_OF_MONTH));
// 在当前年月日上增加
calendarInstance.add(Calendar.MONTH, 1);
System.out.println(calendarInstance.get(Calendar.MONTH));
// 当前是一周的第几天 (从周日算起)
System.out.println(calendarInstance.get(Calendar.DAY_OF_WEEK));
// 当前是今年的第几天
System.out.println(calendarInstance.get(Calendar.DAY_OF_YEAR));
// 当前出在当月的第几周
System.out.println(calendarInstance.get(Calendar.WEEK_OF_MONTH));
}
}

静态常量

常量描述
Calendar.DAY_OF_MONTH月中的某天(1-31)
Calendar.DAY_OF_WEEK一周中的某天(例如,Calendar.MONDAY, Calendar.TUESDAY 等)
Calendar.DAY_OF_YEAR年中的某天(1-366)
Calendar.MONTH月份(Calendar.JANUARY, Calendar.FEBRUARY 等)
Calendar.YEAR年份
Calendar.HOUR小时(12 小时制,0-11)
Calendar.HOUR_OF_DAY小时(24 小时制,0-23)
Calendar.MINUTE分钟(0-59)
Calendar.SECOND秒(0-59)
Calendar.MILLISECOND毫秒(0-999)
Calendar.AM_PM上午或下午(Calendar.AM, Calendar.PM
Calendar.WEEK_OF_MONTH月中的周(1-5)
Calendar.WEEK_OF_YEAR年中的周(1-53)
Calendar.ERA时代(例如,Calendar.AD, Calendar.BC

日历实现

package com.data_calendar_learning;

import java.util.Calendar;

public class CalendarTest {

public static void displayCurrentDate(Calendar calendar) {
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);
}

public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.DAY_OF_MONTH, 1);
int firstWeekday = calendar.get(Calendar.DAY_OF_WEEK);

// 获取当月一共多少天
calendar.add(Calendar.MONTH, 1);
calendar.add(Calendar.DAY_OF_MONTH, -1);
int daysInCurrentMonth = calendar.get(Calendar.DAY_OF_MONTH);
int lastWeekday = calendar.get(Calendar.DAY_OF_WEEK);

// 获取上个月一共多少天
calendar.add(Calendar.MONTH, -1);
int daysInLastMonth = calendar.get(Calendar.DAY_OF_MONTH);

String[] weekdays = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
int[] calendarDays = new int[35];
int index = 0;

// 填充上个月在当前月日历中显示的天数
for (int i = daysInLastMonth - firstWeekday + 2; i <= daysInLastMonth; i++) {
calendarDays[index++] = i;
}

// 填充本月的天数
for (int i = 1; i <= daysInCurrentMonth; i++) {
calendarDays[index++] = i;
}

// 填充下个月在当前月日历中显示的天数
for (int i = 1; i < 7 - lastWeekday; i++) {
calendarDays[index++] = i;
}

// 星期视图
for (String day : weekdays) {
System.out.print(day + "\t");
}
System.out.println();

// 日期视图
for (int i = 0; i < calendarDays.length; i++) {
System.out.print(calendarDays[i] + "\t");
if ((i + 1) % 7 == 0) {
System.out.println();
}
}

displayCurrentDate(calendar);
}
}

提示

在处理日期和时间时,注意时区的影响,必要时使用TimeZone类进行设置,以确保时间的准确性。