日期日历类

5.日期日历类

不同场景使用不同类

简单的计时:System.currentTimeMillis() 
需要一个对象来保存日期:java.util.Date(不赞成使用,用 Calendar 类来代替)
需要读取和写入数据库的日期和时间:java.sql.Date 和 java.sql.Timestamp
需要进行日期计算,月份日期加另一个日期,看给定日期(星期一,星期二等),转换时区之间的日期时间:java .util.GregorianCalendar 跨时区执行日历计算:java.util.TimeZone
​
Java 8 新增 API:
Instant时间线上某一瞬间(秒和纳秒表示)
Duration时间差(秒和纳秒表示)
LocalDate没有时区信息的日期//生日、法定假日  LocalTime没有时区信息的本地时间  LocalDateTime没有时区信息的日期和时间
ZonedDateTime包括时区信息的日期和时间,
DateTimeFormatter将日期时间对象格式化为字符串
1.java.sql.Date 和 java.sql.Timestamp
long time = System.currentTimeMillis();返回自1970年1月1日以来的时间
​
import java.sql.Date;
Date date = new Date(time);             2024-01-19 (没具体时间)
System.out.println(date);
​
import java.sql.Timestamp;
Timestamp timestamp = new Timestamp(time);  2024-01-19 10:55:14.754
System.out.println(timestamp);
2.java.util.Calendar 和 GregorianCalendar
GregorianCalendar 的set get
Calendar calendar = new GregorianCalendar();
int year = calendar.get(Calendar.YEAR);2024
int month = calendar.get(Calendar.MONTH);0                   //1月是0, 不是 1
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);19
int dayOfWeek  = calendar.get(Calendar.DAY_OF_WEEK);
int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
int weekOfMonth= calendar.get(Calendar.WEEK_OF_MONTH);
​
int hour = calendar.get(Calendar.HOUR);             // 12 小时制
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); // 24 小时制
int minute = calendar.get(Calendar.MINUTE);
​
calendar.set(Calendar.MONTH, 11); // 11 = december,十二月
System.out.println(calendar.get(Calendar.MONTH));
年月日等的加减
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.DAY_OF_MONTH, 1);// 加 1 天
calendar.add(Calendar.DAY_OF_MONTH, -1);// 减 1 天
注意
Calendar 类的 MONTH 字段从 0 到 11(0 是一月,11 是十二月)
1 =星期日,2 =星期一,7 =星期六
Calendar/Date/String 互相转换
Calendar calendar = Calendar.getInstance();     // Calendar to Date
java.util.Date date = calendar.getTime(); 
​
calendar.setTime(new java.util.Date());         // Date to Calendar
​
Calendar calendat = Calendar.getInstance();     // Calendar to String
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = sdf.format(calendar.getTime());    
​
String str = "2018-12-3";                       // String to Calendar
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
​
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// Date to String
String dateStr = sdf.format(new Date());
​
String str = "2018-12-3";                       // String to Date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dateParse = sdf.parse(str);
3.java.util.TimeZone
从 Calendar 中获取TimeZone:
Calendar calendar = new GregorianCalendar();
// get
TimeZone timeZone = calendar.getTimeZone();
// set
calendar.setTimeZone(timeZone);
​
创建 TimeZone 对象:
// 获取默认时区对象
TimeZone timeZone = TimeZone.getDefault();
// 获取指定时区对象
TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai");
TimeZone timeZone = TimeZone.getTimeZone("Europe/Copenhagen");
使用
TimeZone timeZoneCN = TimeZone.getTimeZone("Asia/Shanghai");
Calendar calendar = new GregorianCalendar();
calendar.setTimeZone(timeZoneCN); 
System.out.println(calendar.getTimeZone().getDisplayName());        中国标准时间
System.out.println("timeCN = " + calendar.getTimeInMillis(););      timeCN = 1705643666005
System.out.println("hour = " + calendar.get(Calendar.HOUR_OF_DAY)); hour = 13
4.SimpleDateFormat 解析和格式化日期

Date -> String 用 formateDate.format(date)

String -> Date 用formateDate.parse(string)

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  //yyyy-MM-dd HH:mm:ss 
​
String dateString = format.format(new Date());
Date date = format.parse ("2018-12-03");  
5.Instant 表示某一瞬间
Instant now = Instant.now();             // 现在这一瞬间 2024-01-19T06:10:20.629938300Z
Instant later = now.plusSeconds(3);      // 3 秒后的瞬间 2024-01-19T06:10:23.629938300Z
Instant earlier = now.minusSeconds(3);   // 3 秒前的瞬间 2024-01-19T06:10:17.629938300Z
6.Duration 表示时间间隔
Instant first = Instant.now();
Thread.sleep(1000);
Instant second = Instant.now();
Duration duration = Duration.between(first, second);
System.out.println(duration.getSeconds());  //1  还有plusSeconds(long),plusMinutes(long)等方法
7.LocalDate这些
LocalDate localDate1 = LocalDate.now();             2024-01-19
LocalDate localDate2 = LocalDate.of(2018, 11, 11);  2018-11-11      .minusYears(3)
​
LocalTime localTime1 = LocalTime.now();
LocalTime localTime2 = LocalTime.of(21, 30, 59, 11001);
​
LocalDateTime localDateTime1 = LocalDateTime.now();
LocalDateTime localDateTime2 =LocalDateTime.of(2018, 11, 11, 10, 55, 36, 123);
​
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZoneId zoneId = ZoneId.of("UTC+1");
ZonedDateTime zonedDateTime2 = ZonedDateTime.of(2015, 11, 30, 23, 45, 59, 1234, zoneId);
8.DateTimeFormatter 解析和格式化日期

对于7的LocalDate这些,要用DateTimeFormatter进行解析和格式化

LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = now.format(formatter);
System.out.println(formattedDateTime);
  • 21
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值