import java.text.SimpleDateFormat;
import java.util.Calendar;
public class CalendarTest {
public static void main(String[] args) {
// 创建Calendar实例
Calendar calendar = Calendar.getInstance();
// 获取当前日期和时间
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1; // 月份从0开始,所以要加1
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
// 打印当前日期和时间
System.out.println("当前日期和时间: " + year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second);
// 设置特定日期
calendar.set(Calendar.YEAR, 2022);
calendar.set(Calendar.MONTH, Calendar.JANUARY); // 月份是从0开始的
calendar.set(Calendar.DAY_OF_MONTH, 15);
// 添加或减去天数
calendar.add(Calendar.DAY_OF_MONTH, 7); // 添加7天
calendar.add(Calendar.DAY_OF_MONTH, -3); // 减去3天
// 格式化日期
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = dateFormat.format(calendar.getTime());
System.out.println("格式化后的日期: " + formattedDate);
// 获取一周的第几天(星期几),注意星期天是1,星期一是2,以此类推
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println("今天是星期: " + (dayOfWeek + 1));
// 获取一月的第几周
int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
System.out.println("今天是本月的第: " + weekOfMonth + " 周");
// 获取一年的第几周
int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
System.out.println("今天是今年的第: " + weekOfYear + " 周");
// 获取当前月份的最大天数
int maxDayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println("本月的最大天数: " + maxDayOfMonth);
// 获取当前月份的最小天数
int minDayOfMonth = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
System.out.println("本月的最小天数: " + minDayOfMonth);
}
}
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
public class LocalDateTest {
public static void main(String[] args) {
// 获取当前日期
LocalDate currentDate = LocalDate.now();
System.out.println("当前日期: " + currentDate);
// 创建指定日期
LocalDate customDate = LocalDate.of(2022, Month.JANUARY, 15);
System.out.println("自定义日期: " + customDate);
// 获取年、月、日
int year = currentDate.getYear();
int month = currentDate.getMonthValue();
int day = currentDate.getDayOfMonth();
System.out.println("年: " + year + ", 月: " + month + ", 日: " + day);
// 修改日期
LocalDate modifiedDate = currentDate.withYear(2023).withMonth(7);
System.out.println("修改后的日期: " + modifiedDate);
// 添加日期
LocalDate addedDate = currentDate.plus(1, ChronoUnit.WEEKS);
System.out.println("添加日期后: " + addedDate);
// 判断日期先后
boolean isBefore = currentDate.isBefore(customDate);
boolean isAfter = currentDate.isAfter(customDate);
System.out.println("当前日期是否在自定义日期之前: " + isBefore);
System.out.println("当前日期是否在自定义日期之后: " + isAfter);
// 获取下一个月的日期
LocalDate nextMonthDate = currentDate.plusMonths(1);
System.out.println("下一个月的日期: " + nextMonthDate);
// 获取两个日期的日期差(天数)
long daysBetween = currentDate.until(customDate, ChronoUnit.DAYS);
System.out.println("两个日期的天数差: " + daysBetween);
// 获取日期的字符串表示
String dateString = currentDate.toString();
System.out.println("日期的字符串表示: " + dateString);
// 解析字符串为LocalDate对象
String customDateString = "2022-12-20";
LocalDate parsedDate = LocalDate.parse(customDateString);
System.out.println("解析后的日期: " + parsedDate);
// 判断是否是闰年
boolean isLeapYear = currentDate.isLeapYear();
System.out.println("当前日期是否是闰年: " + isLeapYear);
}
}
import java.time.*;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
public class LocalDateTimeTest {
public static void main(String[] args) {
// LocalDate 示例
LocalDate currentDate = LocalDate.now();
System.out.println("当前日期: " + currentDate);
LocalDate customDate = LocalDate.of(2022, Month.JANUARY, 15);
System.out.println("自定义日期: " + customDate);
int year = currentDate.getYear();
int month = currentDate.getMonthValue();
int day = currentDate.getDayOfMonth();
System.out.println("年: " + year + ", 月: " + month + ", 日: " + day);
LocalDate modifiedDate = currentDate.withYear(2023).withMonth(7);
System.out.println("修改后的日期: " + modifiedDate);
LocalDate addedDate = currentDate.plus(1, ChronoUnit.WEEKS);
System.out.println("添加日期后: " + addedDate);
boolean isBefore = currentDate.isBefore(customDate);
boolean isAfter = currentDate.isAfter(customDate);
System.out.println("当前日期是否在自定义日期之前: " + isBefore);
System.out.println("当前日期是否在自定义日期之后: " + isAfter);
LocalDate nextMonthDate = currentDate.plusMonths(1);
System.out.println("下一个月的日期: " + nextMonthDate);
long daysBetween = currentDate.until(customDate, ChronoUnit.DAYS);
System.out.println("两个日期的天数差: " + daysBetween);
String dateString = currentDate.toString();
System.out.println("日期的字符串表示: " + dateString);
String customDateString = "2022-12-20";
LocalDate parsedDate = LocalDate.parse(customDateString);
System.out.println("解析后的日期: " + parsedDate);
boolean isLeapYear = currentDate.isLeapYear();
System.out.println("当前日期是否是闰年: " + isLeapYear);
// LocalDateTime 示例
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("\n当前日期和时间: " + currentDateTime);
LocalDateTime customDateTime = LocalDateTime.of(2022, Month.JANUARY, 15, 12, 30, 45);
System.out.println("自定义日期和时间: " + customDateTime);
int hour = currentDateTime.getHour();
int minute = currentDateTime.getMinute();
int second = currentDateTime.getSecond();
System.out.println("小时: " + hour + ", 分钟: " + minute + ", 秒: " + second);
LocalDateTime modifiedDateTime = currentDateTime.withYear(2023).withMonth(7).withHour(15);
System.out.println("修改后的日期和时间: " + modifiedDateTime);
LocalDateTime addedDateTime = currentDateTime.plus(1, ChronoUnit.HOURS).plusMinutes(30);
System.out.println("添加时间后: " + addedDateTime);
boolean isBeforeDateTime = currentDateTime.isBefore(customDateTime);
boolean isAfterDateTime = currentDateTime.isAfter(customDateTime);
System.out.println("当前日期时间是否在自定义日期时间之前: " + isBeforeDateTime);
System.out.println("当前日期时间是否在自定义日期时间之后: " + isAfterDateTime);
long hoursBetweenDateTime = currentDateTime.until(customDateTime, ChronoUnit.HOURS);
System.out.println("两个日期时间的小时差: " + hoursBetweenDateTime);
String dateTimeString = currentDateTime.toString();
System.out.println("日期时间的字符串表示: " + dateTimeString);
String customDateTimeString = "2022-12-20T15:30:00";
LocalDateTime parsedDateTime = LocalDateTime.parse(customDateTimeString);
System.out.println("解析后的日期时间: " + parsedDateTime);
boolean isLeapYearDateTime = currentDateTime.toLocalDate().isLeapYear();
System.out.println("当前日期时间是否是闰年: " + isLeapYearDateTime);
// 其他 LocalDateTime 示例方法
long nanosecond = currentDateTime.getNano();
System.out.println("纳秒部分: " + nanosecond);
DayOfWeek dayOfWeek = currentDateTime.getDayOfWeek();
System.out.println("星期几: " + dayOfWeek);
Month monthDateTime = currentDateTime.getMonth();
System.out.println("月份: " + monthDateTime);
long milliSecond = currentDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
System.out.println("毫秒表示: " + milliSecond);
int hourOfDay = currentDateTime.get(ChronoField.HOUR_OF_DAY);
System.out.println("小时部分: " + hourOfDay);
LocalDateTime newDateTime = currentDateTime.with(ChronoField.DAY_OF_MONTH, 25);
System.out.println("修改后的日期和时间: " + newDateTime);
LocalDateTime startTime = LocalDateTime.of(2022, 1, 1, 8, 0);
LocalDateTime endTime = LocalDateTime.of(2022, 1, 1, 17, 0);
boolean isWithinRangeDateTime = currentDateTime.isAfter(startTime) && currentDateTime.isBefore(endTime);
System.out.println("当前日期时间是否在工作时间内: " + isWithinRangeDateTime);
}
}
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
public class LocalTimeTest {
public static void main(String[] args) {
// 获取当前时间
LocalTime currentTime = LocalTime.now();
System.out.println("当前时间: " + currentTime);
// 创建指定时间
LocalTime customTime = LocalTime.of(12, 30, 45);
System.out.println("自定义时间: " + customTime);
// 获取小时、分钟、秒
int hour = currentTime.getHour();
int minute = currentTime.getMinute();
int second = currentTime.getSecond();
System.out.println("小时: " + hour + ", 分钟: " + minute + ", 秒: " + second);
// 修改时间
LocalTime modifiedTime = currentTime.withHour(15).withMinute(45);
System.out.println("修改后的时间: " + modifiedTime);
// 添加时间
LocalTime addedTime = currentTime.plusHours(2).plusMinutes(30);
System.out.println("添加时间后: " + addedTime);
// 判断时间先后
boolean isBefore = currentTime.isBefore(customTime);
boolean isAfter = currentTime.isAfter(customTime);
System.out.println("当前时间是否在自定义时间之前: " + isBefore);
System.out.println("当前时间是否在自定义时间之后: " + isAfter);
// 获取下一个小时的时间
LocalTime nextHour = currentTime.plus(1, ChronoUnit.HOURS);
System.out.println("下一个小时的时间: " + nextHour);
// 获取两个时间的时间差(小时)
long hoursBetween = currentTime.until(nextHour, ChronoUnit.HOURS);
System.out.println("两个时间的小时差: " + hoursBetween);
// 获取时间的字符串表示
String timeString = currentTime.toString();
System.out.println("时间的字符串表示: " + timeString);
// 解析字符串为LocalTime对象
String customTimeString = "15:30:00";
LocalTime parsedTime = LocalTime.parse(customTimeString);
System.out.println("解析后的时间: " + parsedTime);
// 判断时间是否在某时间段内
LocalTime startTime = LocalTime.of(8, 0);
LocalTime endTime = LocalTime.of(17, 0);
boolean isWithinRange = currentTime.isAfter(startTime) && currentTime.isBefore(endTime);
System.out.println("当前时间是否在工作时间内: " + isWithinRange);
// 判断时间是否是午夜(凌晨)
boolean isMidnight = currentTime.equals(LocalTime.MIDNIGHT);
System.out.println("当前时间是否是午夜: " + isMidnight);
}
}
java实训第五天
于 2024-01-26 19:40:26 首次发布