看书《java核心技术I》提到:用来保存时间的两个类为Date和LocalDate。
LocalDate类采用的是时间和日历分开,不需要使用构造器来构造一个LocalDate对象,而直接使用静态工厂方法来构造一个对象。
下面就通过LocalDate类,写出一个与日历相当的程序:
实现目标:
package cn.tzs.testLocalDate;
import java.time.*;
public class TestLocalDate {
public static void main(String[] args){
LocalDate date = LocalDate.of(2018,2,12);
int month = date.getMonthValue();
int today = date.getDayOfMonth();
//int year = date.getYear();
//将date设置为当前月的第一天
date = date.minusDays(today-1);
DayOfWeek weekday = date.getDayOfWeek();
int value = weekday.getValue();
System.out.println("Mon Tue Wed Thu Fri Sat Sun");
//当前的星期与实际星期差几,就打打印几个空格占位。
for(int i = 1; i<value;i++){
System.out.print(" ");
}
//只要日期的当前月值为给定的月份值 ,就执行循环体
while(date.getMonthValue() == month){
//格式化输出当前日期在月份中的第几天
System.out.printf("%3d",date.getDayOfMonth());
//当前月份日期与,给定的日期相等时,则在后面加上*
if(date.getDayOfMonth() == today){
System.out.print("*");
}else{
System.out.print(" ");
}
//每打印一个日期后,则把当前日期向后移一天
date = date.plusDays(1);
//如果当前日期,是一个星期的星期一,则打印一个换行符。
if(date.getDayOfWeek().getValue() == 1) System.out.println();
}
if(date.getDayOfWeek().getValue() != 1) System.out.println();
}
}
运行结果,如上图所示。
但是,现在很多手机、电脑上的日历,都是上、下两个月时间,进行联动显示的,如何实现呢?通过百度,发现在java.time.*包中还有一个类,是专门用来处理时间:TemporalAdjusters类。使用方法,如下:
import java.time.temporal.TemporalAdjusters; //必须引入TemporalAdjusters包
// 取当前日期:
LocalDate today = LocalDate.now(); // -> 2014-12-24
// 根据年月日取日期:
LocalDate crischristmas = LocalDate.of(2014, 12, 25); // -> 2014-12-25
// 根据字符串取:
LocalDate endOfFeb = LocalDate.parse("2014-02-28"); // 严格按照ISO yyyy-MM-dd验证,02写成2都不行,当然也有一个重载方法允许自己定义格式
LocalDate.parse("2014-02-29"); // 无效日期无法通过:DateTimeParseException: Invalid date
// 取本月第1天:
LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth()); // 2017-03-01
// 取本月第2天:
LocalDate secondDayOfThisMonth = today.withDayOfMonth(2); // 2017-03-02
// 取本月最后一天,再也不用计算是28,29,30还是31:
LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth()); // 2017-12-31
// 取下一天:
LocalDate firstDayOf2015 = lastDayOfThisMonth.plusDays(1); // 变成了2018-01-01
// 取2017年1月第一个周一,用Calendar要死掉很多脑细胞:
LocalDate firstMondayOf2015 = LocalDate.parse("2017-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); // 2017-01-02
实现目标:上、下月联动的日历:
package cn.tzs.testLocalDate;
import java.time.*;
import java.time.temporal.TemporalAdjusters;
public class TestLocalDate {
public static void main(String[] args){
LocalDate date = LocalDate.of(2018,2,12);
int month = date.getMonthValue();
int today = date.getDayOfMonth();
//int year = date.getYear();
//将date设置为当前月的第一天
date = date.minusDays(today-1);
DayOfWeek weekday = date.getDayOfWeek();
int value = weekday.getValue();
System.out.println("Mon Tue Wed Thu Fri Sat Sun");
//当前的星期与实际星期差几,就打打印几个空格占位。
//如何实现打印上月的几天呢?
LocalDate up = date.minusMonths(1);
int last = up.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
//System.out.println(last);
//System.out.println(value);
for(int i=value-1; i>0 ; i--){
System.out.printf("%3d",last-i+1);
System.out.print(" ");
//System.out.print(last-i);
}
/*
for(int i = 1; i<value;i++){
System.out.print(" ");
}
*/
//只要日期的当前月值为给定的月份值 ,就执行循环体
while(date.getMonthValue() == month){
//格式化输出当前日期在月份中的第几天
System.out.printf("%3d",date.getDayOfMonth());
//当前月份日期与,给定的日期相等时,则在后面加上*
if(date.getDayOfMonth() == today){
System.out.print("*");
}else{
System.out.print(" ");
}
//每打印一个日期后,则把当前日期向后移一天
date = date.plusDays(1);
//如果当前日期,是一个星期的星期一,则打印一个换行符。
if(date.getDayOfWeek().getValue() == 1) System.out.println();
}
if(date.getDayOfWeek().getValue() != 1) System.out.println();
}
}