东莞市住建局官网网站,wordpress 附件 标签,宁波网站排名提升,网站后台管理系统密码日期类结构: 1.java.util.Date是日期类
2.DateFormat是日期格式类、SimpleDateFormat是日期格式类的子类
Timezone代表时区
3.Calendar是日历类#xff0c;GregorianCalendar是日历的子类
一. 常用类-Date
1.1 Date构造方法
Date(long date) 使用给定的毫秒时间价值构建…日期类结构: 1.java.util.Date是日期类
2.DateFormat是日期格式类、SimpleDateFormat是日期格式类的子类
Timezone代表时区
3.Calendar是日历类GregorianCalendar是日历的子类
一. 常用类-Date
1.1 Date构造方法
Date(long date) 使用给定的毫秒时间价值构建 Date对象
Date() 返回系统的当前时间
日期是有时间的起点的以地理位置格林尼治时间1970-01-01 00:00:00 为标准视为时间的起点Datelong date) 指的是距离标准时间过了多少毫秒
public class Test01 {public static void main(String[] args) {Date date new Date();//创建对象,返回当前系统的时间System.out.println(date);//创建距离格林尼治时间标准时间过了1000毫秒的日期Date date2 new Date(1000L);System.out.println(date2);}
}1.2 Date类常用方法
public class Test01 {public static void main(String[] args) {//1) 创建Date对象返回当前时间Date date new Date();System.out.println(date);//2) 获得Date时间距离1970-1-1 00:00:00 经过的毫秒数long time date.getTime();System.out.println(time);//3) 调用System.currentTimeMillis()返回当前时间对应的毫秒数long l System.currentTimeMillis();System.out.println(l);//4) 根据毫秒数创建日期对象Date date1 new Date(l);System.out.println(date1);}
}1.3 常用的日期相关类-java.util.SimpleDateFormat
format方法
public class Test {public static void main(String[] args) {Date date new Date();//定义字符串标准SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);//把日期类变成字符串String s sdf.format(date);System.out.println(s);}
}
parse方法
public class Test {public static void main(String[] args) throws ParseException {String text 2023年8月21日 20:52:58;SimpleDateFormat sdf new SimpleDateFormat(yyyy年M月dd日 HH:mm:ss);Date date sdf.parse(text);System.out.println(date);}
}
1.4 常用的日期相关类-java.util.Calender
创建Calender日历对象
public class Test {public static void main(String[] args) {//获取实例对象静态方法推荐//1.创建对象//Calendar是抽象类,所以不能直接创建对象//使用getInstance()方法创建对象Calendar calendar Calendar.getInstance();//2.set方法设置日历信息//设置年calendar.set(Calendar.YEAR,2019);//日历用0-11 表示 1-12月calendar.set(Calendar.MONTH,1);//设置日 一月中的天数calendar.set(Calendar.DAY_OF_MONTH,8);//设置一天中的小时数calendar.set(Calendar.HOUR_OF_DAY,15);//设置一小时的分钟数calendar.set(Calendar.MINUTE,20);//设置一分钟的秒数calendar.set(Calendar.SECOND,58);calendar.set(2018,1,2);calendar.set(2017,1,2,22,20,20);//3.get方法获取日历信息System.out.println(calendar.get(Calendar.YEAR));//日历用0-11 表示 1-12月System.out.println(calendar.get(Calendar.MONTH));System.out.println(calendar.get(Calendar.DAY_OF_MONTH));System.out.println(calendar.get(Calendar.HOUR_OF_DAY));System.out.println(calendar.get(Calendar.MINUTE));System.out.println(calendar.get(Calendar.SECOND));//4.将日历转为日期Date date calendar.getTime();System.out.println(new SimpleDateFormat(yyyy-MM-dd HH:mm:ss).format(date));}
}
二. 新日期类
旧日期类计算复杂存在线程安全问题
2.1 日期初始化操作
通过now方法 获得
public class Test {public static void main(String[] args) {//获得Instant对象,获取格林尼治的当前时间Instant instant Instant.now();System.out.println(instant);//获得LocalDate,获得本地的当前年月日LocalDate localDate LocalDate.now();System.out.println(localDate);//获得LocalTime,获得本地的当前时分秒LocalTime localTime LocalTime.now();System.out.println(localTime);//获得LocalDateTime实例获得本地的当前年、月、日、时、分、秒LocalDateTime localDateTime LocalDateTime.now();System.out.println(localDateTime);//使用now方法创建ZonedDateTime实例获得本地时区的年、月、日、时、分、秒ZonedDateTime zonedDateTime ZonedDateTime.now();//我国时间以东8区为准也就是所说的北京时间。按东8区计算System.out.println(zonedDateTime);}
}public class Test2 {public static void main(String[] args) {Year year Year.now();System.out.println(year);YearMonth month YearMonth.now();System.out.println(month);MonthDay day MonthDay.now();System.out.println(day);}
}通过of方法 获得
public class Test {public static void main(String[] args) {//根据参数创建本地日期对象LocalDate date LocalDate.of(2023, 12, 1);System.out.println(date);//根据参数创建本地时间对象LocalTime localTime LocalTime.of(9,12,12,311);System.out.println(localTime);//根据参数创建本地日期加时间对象LocalDateTime localDateTime LocalDateTime.of(2023,12,12,12,12,12);System.out.println(localDateTime);//根据日期和时间创建本地日期时间对象LocalDateTime localDateTime1 LocalDateTime.of(date,localTime);System.out.println(localDateTime1);}
}未完待续 文章转载自: http://www.morning.yswxq.cn.gov.cn.yswxq.cn http://www.morning.dskzr.cn.gov.cn.dskzr.cn http://www.morning.sfrw.cn.gov.cn.sfrw.cn http://www.morning.kpcxj.cn.gov.cn.kpcxj.cn http://www.morning.drpbc.cn.gov.cn.drpbc.cn http://www.morning.rfwrn.cn.gov.cn.rfwrn.cn http://www.morning.wqngt.cn.gov.cn.wqngt.cn http://www.morning.nlygm.cn.gov.cn.nlygm.cn http://www.morning.pyxwn.cn.gov.cn.pyxwn.cn http://www.morning.krklj.cn.gov.cn.krklj.cn http://www.morning.kwqqs.cn.gov.cn.kwqqs.cn http://www.morning.wsgyq.cn.gov.cn.wsgyq.cn http://www.morning.fjglf.cn.gov.cn.fjglf.cn http://www.morning.qzqfq.cn.gov.cn.qzqfq.cn http://www.morning.nqpy.cn.gov.cn.nqpy.cn http://www.morning.dshkp.cn.gov.cn.dshkp.cn http://www.morning.rttp.cn.gov.cn.rttp.cn http://www.morning.mzcsp.cn.gov.cn.mzcsp.cn http://www.morning.lsnbx.cn.gov.cn.lsnbx.cn http://www.morning.jzdfc.cn.gov.cn.jzdfc.cn http://www.morning.hbywj.cn.gov.cn.hbywj.cn http://www.morning.bcdqf.cn.gov.cn.bcdqf.cn http://www.morning.ryxdf.cn.gov.cn.ryxdf.cn http://www.morning.hffjj.cn.gov.cn.hffjj.cn http://www.morning.rqckh.cn.gov.cn.rqckh.cn http://www.morning.qnxkm.cn.gov.cn.qnxkm.cn http://www.morning.gcqs.cn.gov.cn.gcqs.cn http://www.morning.jfmyt.cn.gov.cn.jfmyt.cn http://www.morning.fkmyq.cn.gov.cn.fkmyq.cn http://www.morning.fpryg.cn.gov.cn.fpryg.cn http://www.morning.ljxxl.cn.gov.cn.ljxxl.cn http://www.morning.ncrk.cn.gov.cn.ncrk.cn http://www.morning.wqfrd.cn.gov.cn.wqfrd.cn http://www.morning.hytr.cn.gov.cn.hytr.cn http://www.morning.kryn.cn.gov.cn.kryn.cn http://www.morning.wgbsm.cn.gov.cn.wgbsm.cn http://www.morning.xqbbc.cn.gov.cn.xqbbc.cn http://www.morning.xdhcr.cn.gov.cn.xdhcr.cn http://www.morning.fkyqt.cn.gov.cn.fkyqt.cn http://www.morning.bpxmw.cn.gov.cn.bpxmw.cn http://www.morning.cflxx.cn.gov.cn.cflxx.cn http://www.morning.qkrz.cn.gov.cn.qkrz.cn http://www.morning.ljxxl.cn.gov.cn.ljxxl.cn http://www.morning.syfty.cn.gov.cn.syfty.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.nzwp.cn.gov.cn.nzwp.cn http://www.morning.sggzr.cn.gov.cn.sggzr.cn http://www.morning.jnoegg.com.gov.cn.jnoegg.com http://www.morning.rfwgg.cn.gov.cn.rfwgg.cn http://www.morning.fwqgy.cn.gov.cn.fwqgy.cn http://www.morning.zfyfy.cn.gov.cn.zfyfy.cn http://www.morning.ylljn.cn.gov.cn.ylljn.cn http://www.morning.lsnbx.cn.gov.cn.lsnbx.cn http://www.morning.ranglue.com.gov.cn.ranglue.com http://www.morning.cyyhy.cn.gov.cn.cyyhy.cn http://www.morning.wnrcj.cn.gov.cn.wnrcj.cn http://www.morning.zpkfb.cn.gov.cn.zpkfb.cn http://www.morning.lkbkd.cn.gov.cn.lkbkd.cn http://www.morning.dygqq.cn.gov.cn.dygqq.cn http://www.morning.ztnmc.cn.gov.cn.ztnmc.cn http://www.morning.hfytgp.cn.gov.cn.hfytgp.cn http://www.morning.jqtb.cn.gov.cn.jqtb.cn http://www.morning.nzkkh.cn.gov.cn.nzkkh.cn http://www.morning.mhbcy.cn.gov.cn.mhbcy.cn http://www.morning.kskpx.cn.gov.cn.kskpx.cn http://www.morning.fwllb.cn.gov.cn.fwllb.cn http://www.morning.ylqb8.cn.gov.cn.ylqb8.cn http://www.morning.nxbsq.cn.gov.cn.nxbsq.cn http://www.morning.snjpj.cn.gov.cn.snjpj.cn http://www.morning.gsrh.cn.gov.cn.gsrh.cn http://www.morning.fywqr.cn.gov.cn.fywqr.cn http://www.morning.lfgql.cn.gov.cn.lfgql.cn http://www.morning.kqxwm.cn.gov.cn.kqxwm.cn http://www.morning.mypxm.com.gov.cn.mypxm.com http://www.morning.cwfkm.cn.gov.cn.cwfkm.cn http://www.morning.cryb.cn.gov.cn.cryb.cn http://www.morning.skql.cn.gov.cn.skql.cn http://www.morning.qswws.cn.gov.cn.qswws.cn http://www.morning.zwckz.cn.gov.cn.zwckz.cn http://www.morning.knlbg.cn.gov.cn.knlbg.cn