Date
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class DateDemo {
public static void main(String[] args) {
Date date = new Date();
// 日期字符串格式
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println("当前时间为: " + ft.format(date));
// 日期属性
// b的使用,月份简称
String str = String.format(Locale.US, "英文月份简称:%tb", date);
System.out.println(str);
System.out.printf("本地月份简称:%tb%n", date);
// B的使用,月份全称
str = String.format(Locale.US, "英文月份全称:%tB", date);
System.out.println(str);
System.out.printf("本地月份全称:%tB%n", date);
// a的使用,星期简称
str = String.format(Locale.US, "英文星期的简称:%ta", date);
System.out.println(str);
// A的使用,星期全称
System.out.printf("本地星期的简称:%tA%n", date);
// C的使用,年前两位
System.out.printf("年的前两位数字(不足两位前面补0):%tC%n", date);
// y的使用,年后两位
System.out.printf("年的后两位数字(不足两位前面补0):%ty%n", date);
// j的使用,一年的天数
System.out.printf("一年中的天数(即年的第几天):%tj%n", date);
// m的使用,月份
System.out.printf("两位数字的月份(不足两位前面补0):%tm%n", date);
// d的使用,日(二位,不够补零)
System.out.printf("两位数字的日(不足两位前面补0):%td%n", date);
// e的使用,日(一位不补零)
System.out.printf("月份的日(前面不补0):%te", date);
// 将日期字符串解析为时间
String input = args.length == 0 ? "2011-11-11 11:11:11" : args[0];
System.out.print(input + " Parses as ");
Date t;
try {
t = ft.parse(input);
System.out.println(t);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Calendar
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CalendarDemo {
public static void main(String[] args) {
// 规定输出日期格式
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
// 创建Calendar对象
Calendar c1 = Calendar.getInstance();
System.out.println(ft.format(c1.getTime()));
// 获得年
int year = c1.get(Calendar.YEAR);
System.out.println(year);
// 获得月,1月为0
int month = c1.get(Calendar.MONTH) + 1;
System.out.println(month);
// 获得日
int day = c1.get(Calendar.DATE);
System.out.println(day);
// 获得时
int hour = c1.get(Calendar.HOUR_OF_DAY);
System.out.println(hour);
// 获得分
int minute = c1.get(Calendar.MINUTE);
System.out.println(minute);
// 获得秒
int second = c1.get(Calendar.SECOND);
System.out.println(second);
// 获得星期几(注意(这个与Date类是不同的):1代表星期日、2代表星期1、3代表星期二,以此类推)
int weekday = c1.get(Calendar.DAY_OF_WEEK);
System.out.println(weekday);
// 通过Calendar生成Date
Date date = c1.getTime();
// 根据date调整Calendar
Calendar c2 = Calendar.getInstance();
c2.setTime(date);
System.out.println(ft.format(c2.getTime()));
// 手动设置时间
c2.set(2022, Calendar.AUGUST, 31);
System.out.println(ft.format(c2.getTime()));
// 测试add()功能
c2.add(Calendar.MONTH, 6);
System.out.println(ft.format(c2.getTime()));
// 测试roll()功能
c2.set(2022, Calendar.AUGUST, 31);
c2.roll(Calendar.MONTH, 6);
System.out.println(ft.format(c2.getTime()));
// 设置MONTH为13
c2.set(Calendar.MONTH, 13);
System.out.println(ft.format(c2.getTime()));
// 关闭容错性
c2.setLenient(false);
try {
c2.set(Calendar.MONTH, 13);
System.out.println(ft.format(c2.getTime()));
} catch (IllegalArgumentException e) {
System.out.println("增加月份错误");
}
// 开启容错性
c2.setLenient(true);
}
}
java.time
Clock
import java.time.Clock;
import java.time.ZoneId;
/**
* Clock用于获取指定时区的当前日期和时间
*/
public class ClockDemo {
public static void main(String[] args) {
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("ZoneId: " + zoneId);
Clock clock = Clock.system(zoneId);
System.out.println(clock.instant());
System.out.println(clock.millis());
System.out.println(System.currentTimeMillis());
}
}
Duration
import java.time.Clock;
import java.time.Duration;
/**
* Duration表示持续时间,代表一段时间
*/
public class DurationDemo {
public static void main(String[] args) {
Duration duration = Duration.ofSeconds(6000);
System.out.println(duration.toMinutes());
System.out.println(duration.toHours());
System.out.println(duration.toDays());
Clock clock = Clock.offset(Clock.systemUTC(), duration);
System.out.println(clock.instant());
}
}
Instant
import java.time.Duration;
import java.time.Instant;
/**
* Instant代表某一个具体的时刻,可以精确到纳秒
*/
public class InstantDemo {
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println(instant);
instant = instant.plusSeconds(6000);
System.out.println(instant);
instant = Instant.parse("2022-08-01T09:08:07.342Z");
System.out.println(instant);
instant = instant.plus(Duration.ofHours(3).plusMinutes(20));
System.out.println(instant);
instant = instant.minus(Duration.ofDays(4));
System.out.println(instant);
}
}
LocalDate
import java.time.LocalDate;
import java.time.Month;
/**
* LocalDate表示不带时区的日期
*/
public class LocalDateDemo {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
// 获取2021年第211天
localDate = LocalDate.ofYearDay(2021, 211);
System.out.println(localDate);
// 2022-08-02
localDate = LocalDate.of(2022, Month.AUGUST, 2);
System.out.println(localDate);
}
}
import java.time.LocalDateTime;
/**
* LocalDateTime代表不带时区的日期时间
*/
public class LocalDateTimeDemo {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
localDateTime = localDateTime.plusDays(3).plusHours(25).plusMinutes(33);
System.out.println(localDateTime);
}
}
LocalTime
import java.time.LocalTime;
/**
* LocalTime代表不带时区的时间
*/
public class LocalTimeDemo {
public static void main(String[] args) {
LocalTime localTime = LocalTime.now();
System.out.println(localTime);
localTime = LocalTime.of(23, 23);
System.out.println(localTime);
localTime = LocalTime.ofSecondOfDay(66666);
System.out.println(localTime);
}
}
Year、Month、Day
import java.time.Month;
import java.time.MonthDay;
import java.time.Year;
import java.time.YearMonth;
/**
* Year代表年
* YearMonth代表年月
* MonthDay代表月日
* Month是一个枚举类
* DayOfWeek也是枚举类
*/
public class YearMonthDayDemo {
public static void main(String[] args) {
Year year = Year.now();
System.out.println(year);
year = year.plusYears(3);
System.out.println(year);
YearMonth yearMonth = year.atMonth(Month.AUGUST);
System.out.println(yearMonth);
yearMonth = yearMonth.minusYears(2).plusMonths(1);
System.out.println(yearMonth);
MonthDay monthDay = MonthDay.now();
System.out.println(monthDay);
monthDay = monthDay.with(Month.APRIL).withDayOfMonth(22);
System.out.println(monthDay);
}
}
ZoneOffset
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.List;
import java.util.stream.Collectors;
public class ZoneOffsetDemo {
public static void main(String[] args) {
ZoneOffset plusTwo = ZoneOffset.ofHours(2);
List<String> zonesWithPlusTwo =
ZoneId.getAvailableZoneIds()
.stream()
.filter(zoneId -> ZoneId.of(zoneId)
.getRules()
.getOffset(Instant.now())
.equals(plusTwo))
.sorted()
.collect(Collectors.toList());
zonesWithPlusTwo.forEach(System.out::println);
}
}