java 日期操作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java 日期操作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4098字,纯文字阅读大概需要6分钟。
内容图文
![java 日期操作](/upload/InfoBanner/zyjiaocheng/614/b9370f6f3db44619922605f818b26122.jpg)
日期操作
1.LocalDate类
获取年月日
```java
public static void main(String[] args) {
//创建了LocalDate对象
LocalDate date=LocalDate.now();
int year=date.getYear();
int month=date.getMonthValue();
int day=date.getDayOfMonth();
//年月日
System.out.println(year+"-"+month+"-"+day);
//查看当前月份有多少天
int daylen=date.lengthOfMonth();
System.out.println(daylen);
//查看当前年是否是闰年
System.out.println(date.isLeapYear());
}
public static void main(String[] args) {
LocalDate date=LocalDate.now();
//以枚举为参获取年月日
int year=date.get(ChronoField.YEAR);
int month=date.get(ChronoField.MONTH_OF_YEAR);
int day=date.get(ChronoField.DAY_OF_MONTH);
//获取本月的第几周
int weekCount=date.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH);
System.out.println(year+"-"+month+"-"+day);
System.out.println(weekCount);
}
2.SimpleDateFormat、DateTimeFormatter日期格式化
public static void main(String[] args) {
Date date=new Date();
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
SimpleDateFormat simpleDateFormat2=new SimpleDateFormat("yyyy/MM/dd日 HH:mm:ss");
System.out.println(date);
String time1=simpleDateFormat.format(date);
System.out.println(time1);
}
public static void main(String[] args) {
LocalDate ldate=LocalDate.now();
String str=ldate.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(str);
//创建日期格式
DateTimeFormatter df=DateTimeFormatter.ofPattern("yyy年MM月dd");
String str1=ldate.format(df);
System.out.println(str1);
//LocalTime 时分秒
//LocalDateTime 年月日时分秒
}
3.LocalTime类
public static void main(String[] args) {
LocalTime time=LocalTime.now();
//获取到当前的小时,分钟,秒
int hour=time.getHour();
int minute=time.getMinute();
int second=time.getSecond();
System.out.println(hour+"时"+minute+"分"+second+"秒");
//DateTimFormatter转换成HH时mm分ss秒
DateTimeFormatter dtf=DateTimeFormatter.ofPattern("HH时mm分ss秒");
String str2=time.format(dtf);
System.out.println(str2);
}
4.LocalDateTime类
public static void main(String[] args) {
//获取当前时间的年月日时分秒
LocalDateTime time=LocalDateTime.now();
int year=time.getYear();
int month=time.getMonthValue();
int day=time.getDayOfMonth();
int hour=time.getHour();
int mm=time.getMinute();
int ss=time.getSecond();
String string=time.format(DateTimeFormatter.ofPattern("yyy年MM月/dd日HH时 mm分 ss秒"));
System.out.println(string);
}
5.Duration\Period求时间差
public static void main(String[] args) {
LocalDateTime d1=LocalDateTime.of(2000,1,1,1,0);
LocalDateTime d2=LocalDateTime.now();
//求时间差
Duration duration=Duration.between(d1, d2);
//换算成天
long days=duration.toDays();
System.out.println("天:"+days);
LocalDate d3=LocalDate.of(1999,5,10);
LocalDate d4=LocalDate.now();
Period period=Period.between(d3, d4);
int day =period.getDays();
int month=period.getMonths();
System.out.println("相差天数:"+day);
}
6.isBefore\isAfter比较时间
public static void main(String[] args) {
LocalDateTime d1=LocalDateTime.of(1999,1,1, 0, 0);
LocalDateTime d2=LocalDateTime.of(2020,1,1, 0, 0);
System.out.println("判断两个时间是否相等:"+d1.isEqual(d2));//false
System.out.println("d1比d2时间更早吗?"+d1.isBefore(d2));//true
System.out.println("d1比22时间更晚吗?"+d1.isAfter(d2));//false
}
7.MonthDay类
public static void main(String[] args) {
LocalDate date=LocalDate.of(2020, 12, 21);
Month month= date.getMonth();
int day=date.getDayOfMonth();
//monthday用于存储月\日
MonthDay monthDay=MonthDay.of(month, day);
System.out.println(monthDay.getMonth());//输出结果为:DECEMBER(12月)
//获取到当前日期的月\日
MonthDay currMonthDay=MonthDay.from(LocalDate.now());
System.out.println(currMonthDay);
}
Java中的println和printf的区别在于:
println是用于输出参数内容,然后换行,其参数个数固定为一个。
printf是用于输出带各种数据类型的占位符的参数,其参数个数是不定的。
用println和printf输出时间
public static void main(String[] args) {
//当前时间:年月日时分秒
//方法1
Date date1 = new Date( );
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
System.out.println("当前时间为: " + ft.format(date1));
//方法2
Date date2 = new Date();
System.out.printf("%tF%n",date2);
System.out.printf("%tT%n",date2);
}
内容总结
以上是互联网集市为您收集整理的java 日期操作全部内容,希望文章能够帮你解决java 日期操作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】