博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java日期时间处理
阅读量:4345 次
发布时间:2019-06-07

本文共 3416 字,大约阅读时间需要 11 分钟。

一.简介

1.java中常用的日期有三种(精确到毫秒):
A.long--毫秒数
B.Date--日期
C.Calendar--日历、抽象类
2.Java中日期经常使用以下五个方面:
A.创建日期
B.日期格式化显示
C.日期的转换(主要是和字符串之间的相互转换)
D.日期中年.月.日.时.分.秒.星期.月份等获取。
E.日期的大小比较.日期的加减。
二.创建时间:
1.long:

long now = System.currentTimeMillis();

2.Date:

Date now = new Date(); //默认为当前时间DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String nowStr = sdf.format(now); System.out.println("格式化后的日期:" + nowStr);System.out.println("Date类型日期:" + now);System.out.println("当前时间毫秒数:" + now.getTime()); //返回Date类型

3.Calendar:

Calendar now1 = Calendar.getInstance(); //默认为当前时间now1.setTimeZone(TimeZone.getTimeZone("GMT+8:00")); //设置时区 北京时间now1.set(2016, 10, 30, 15, 55, 44); //陷阱:Calendar的月份是0~11Calendar now2 = new GregorianCalendar(); //默认为当前时间Calendar now3 = new GregorianCalendar(2016, 10, 30, 15, 55, 44); //陷阱:Calendar的月份是0~11

4.获取时间:

System.out.println("年: " + now.get(Calendar.YEAR)); //返回int类型System.out.println("月: " + (now.get(Calendar.MONTH) + 1)); //返回int类型,注意:月份从零开始System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH)); //返回int类型System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY)); //返回int类型System.out.println("分: " + now.get(Calendar.MINUTE)); //返回int类型System.out.println("秒: " + now.get(Calendar.SECOND)); //返回int类型System.out.println("Date类型日期:" + now.getTime()); //返回Date类型System.out.println("当前时间毫秒数:" + now.getTimeInMillis()); //返回long类型

5.注意:

A.Calendar的星期是从周日开始的,常量值为1。

B.Calendar的月份是从一月开始的,常量值为0。
三.格式转换
1.long转换成Date
A.

new Date(long);

B.

Date date = new Date();date.setTime(long);

2.Date转换成Calendar

Calendar cal = Calendar.getInstance();cal.setTime(date);

3.Date转换成long

long l = date.getTime();

4.Calendar转换成Date

Date date = cal.getTime();

5.Date转换成String

Date now = new Date();DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String nowStr = sdf.format(now);

6.String转换成Date

DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = sdf.parse("2016-2-17 11:20:50");

四.日期比较大小

1.Date:
A.before.after.equals
B.

date1.compareTo(date2) //返回值小于0表示小于,等于0表示等于,大于0表示大于

2.Calendar:

A.before.after.equals

B.

cal1.compareTo(cal2) //返回值小于0表示小于,等于0表示等于,大于0表示大于

3.计算时间差:(Date和Calendar都转换成long来进行比较)

long between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒long day=between/(24*3600);long hour=between%(24*3600)/3600;long minute=between%3600/60;long second=between%60/60;System.out.println(""+day+"天"+hour+"小时"+minute+"分"+second+"秒");

五.日期加减

1.

Calendar cal = Calendar.getInstance();cal.add(Calendar.YEAR, -1);System.out.println("去年是" + cal.get(Calendar.YEAR + "年"));

2.每月最后一天(Actual:实际的)

Calendar cal = Calendar.getInstance();int maxDay=cals.getActualMaximum(Calendar.DAY_OF_MONTH);DateFormat formatter3=new SimpleDateFormat("yyyy-MM-"+maxDay);System.out.println(formatter3.format(cal.getTime()));

六.附

1.compareTo是Compareable接口的一个方法,主要用于规定创建对象的大小关系,该对象要实现compareable接口,当a.compareTo(b)>0时,则a>b,当a.compareTo(b)<0时,a<b,当a.compareTo(b)=0时,a=b。
2.选择Comparable接口还是Comparator?
Comparable:一个类实现了Comparable接口则表明这个类的对象之间是可以相互比较的,这个类对象组成的集合就可以直接使用sort方法排序。用Comparable简单, 只要实现Comparable 接口的对象直接就成为一个可以比较的对象,但是需要修改源代码。
Comparator:Comparator可以看成一种算法的实现,将算法和数据分离。用Comparator的好处是不需要修改源代码,而是另外实现一个比较器,当某个自定义的对象需要作比较的时候,把比较器和对象一起传递过去就可以比大小了。
3.延时方法:
A.

Thread.currentThread().sleep(500);

B.

Timer timer=new Timer();//实例化Timer类 更精确timer.schedule(new TimerTask(){  public void run(){    System.out.println("退出");    this.cancel();  }},500);

 

转载于:https://www.cnblogs.com/lovgge/p/5196062.html

你可能感兴趣的文章
手把手玩转win8开发系列课程(11)
查看>>
Linux Namespace : User
查看>>
交换两个整形变量的数值
查看>>
Linux----常用操作
查看>>
sequence
查看>>
Delphi错误:Stack overflow的解决方法
查看>>
一篇很全面的freemarker教程
查看>>
取消chrome(谷歌浏览器)浏览器下最小字体限制
查看>>
模板方法模式
查看>>
什么是ECC内存?
查看>>
使用Visual Studio 2013进行UI自动化测试
查看>>
13-集体照
查看>>
读了曾国藩家书,,心态逐渐平和起来。搞技术的如果缺乏信念的指引,生活会很乏味无聊!...
查看>>
前端javascript 错误 Uncaught SyntaxError: Unexpected token ILLEGAL
查看>>
Selenium WebDriver问题--无法打开Chrome浏览器
查看>>
2017.4.18 Java的Integer与int互转
查看>>
小程序接受返回数组的坑
查看>>
Arduino---HC-05 蓝牙模块
查看>>
构建之法读书笔记02——个人技术和流程
查看>>
解决VS2015安装Android SDK 后文件不全及更新问题
查看>>