日期相关
java中日期处理;早期api 一般使用这两个类;
DateFormat类和SimpleDateFormat类
public class SimpleDateFormat extends DateFormat 是一个以与语言环境相关的方式格式化和分析日期的具体类(日期时间输出的工具类)
它允许进行日期格式化(日期–》文本)、分析(文本-》日期)和规范。所以simpleDateFormat类可以实现:String 到Date ,Date到String 的互转
日期类之间的互转
/**Date Long Calender*/
Date 类对象与long 型时间的互转
//1.将Date类的对象转换成long型时间
Date d = new Date();
//使用对象的getTime()方法完成
long dLong =d.getTime();
//2.将long 型时间转换为Date 类的对象
long time =1290876532190L;
//使用Date的构造方法完成
Date d2 =new Date(time);
Calender 对象和long 型时间之间的互转
/将Calender对象转换为相对时间
Calendar c = Calendar.getInstance();
long t1 = c.getTimeInMillis();
// 将long转换为Calendar对象
Calendar c1 = Calendar.getInstance();
long t = 1252785271098L;
c1.setTimeInMillis(t1);
/* String Date Calendar 三者之间的相互转换*/
Calendar对象和Date对象之间的互转
//1 .将Calendar对象转换为Date(c.getTime())
Calendar c = Calendar.getInstance();
Date d = c.getTime();
// 2.将Date转换为Calendar对象(s.setTime(date))
Calendar c1 = Calendar.getInstance();
Date d1 = new Date();
//通过setTime()方法后,日历c1所表示的日期就d1的日期
c1.setTime(d1);
String Calendar之间的转换:
//1.Date 转化String
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String dateStr=sdf.format(new Date());
//2.String 转化Date
String str="2010-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date birthday = sdf.parse(str);
Date个Calendar对象之间的转换
//1.Date 转化为Calendar
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
//2.Calenda转换为Date
Calendar calendar = Calendar.getInstance();
Date date =calendar.getTime();
Date和String对象之间的转换
//1.Calendar 转化 String
Calendar calendat = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd")0;
String dateStr = sdf.format(calendar.getTime());
//2.String 转化Calendar
String str="2010-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
## 计算机中的时间处理
统一概念
首先先理解一些通用的概念
标准时间
- **UTC**(Universal Time Coordinated),即协调世界时。全世界统一的世界标准时间。需要不规则地加入闰秒。
- GMT
(Greenwich Mean Tim),格林尼治平均时间。
- 一般认为UTC和GMT是相等的,但是会存在0.9秒以内的误差,这是由于地球不规则自转引起的。
### 时区
- **本地时间**,UTC+时区。东为正西为负。比如北京时间在东八区就是 UTC+(+0800)。
- **DST**,夏令时。是指夏天太阳升起比较早,将时钟拨快一个小时来提早日光的使用。欧美主要国家都引用了这个做法。如果在夏令时时区内 DST=UTC+时区+1。
### 时间戳!表示当前时间到1970年1月1日00:00:00 UTC对应的**秒数**。
时间戳**,示当前时间到1970年1月1日00:00:00 UTC对应的**毫秒数**。
## 字符串的表示
为了让时间易于阅读又出现用各种字符串来表示时间的方法
#### RFC2822
YYYY/MM/DD HH:MM:SS + timezone (时区用4位数字表示)
//eg 1992/02/12/12:23:22 +0800
#### ISO 8601
YYYY-MM-DDThh:mm:ss+ timezone(时区用HH:MM表示)
1997-07-16T08:20:30Z
//"Z"表示UTC标准时区,即“00:00”,所以这里表示零时区的1997年7月16日08时20分30秒
//转换成位于东八区的北京时间为1997年7月17日16时间20分30秒
//转换成位于东八区的北京时间则为1997年7月17日16时20分30秒
1997-07-16T19:20:30+01:00
//表示东一区的1997年7月16日19时20秒30分,转换成UTC标准时间的话是1997-07-16T18:20:302
#### java8中表示日期的时间的类有多个,主要的有。
instant :它表示的是时间戳,表示时刻,不直接对应年月日信息,需要通过时区转换
localDateTime:表示与时区无关的日期和时间信息,不直接对应时刻,需要通过时区转换
LocalDate:表示与时区无关日期和时间信息,不直接对应时刻,需要通过时区转换
localTime :表示与时区无关的时间,与LocalDateTime相比,只有日期信息,没有日期信息
ZoneDateTime : 表示特定时区的日期和时间
ZoneId/ZoneOffset:表示时区
1.LocalDate转Date
LocalDate nowLocalDate = LocalDate.now();
Date date = Date.from(localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
2.LocalDateTime转Date
LocalDateTime localDateTime = LocalDateTime.now();
Date date = Date.from(localDateTime.atZone(ZoneOffset.ofHours(8)).toInstant());
3.Date转LocalDateTime(LocalDate)
Date date =newDate();
LocalDateTime localDateTime = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
LocalDate localDate = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDate();
4.LocalDate转时间戳
LocalDate localDate = LocalDate.now();
longtimestamp = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
5.LocalDateTime转时间戳
LocalDateTime localDateTime = LocalDateTime.now();
longtimestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
6.时间戳转LocalDateTime(LocalDate)
longtimestamp = System.currentTimeMillis();
LocalDate localDate = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDate();
LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();