首页 > 编程知识 正文

java设置时间,java 当前日期

时间:2023-05-04 19:47:01 阅读:149688 作者:1701

简单使用时间API (局部详细信息)

java获取当前日期和时间(各种方法的比较)

文章目录一、满意短靴,使用1、获取当前时间毫秒值2、获取当前时间3、获取当前日期(对月) 4、SimpleDateFormat格式化字符含义5、YYYY和YYYY区别3、JDK1.8中date/time appad 轻松使用Calendar 1、获取相关日期元素2、设置第3天、计算日期(添加/减去日期) 4、转换日历对象和时间对象5、转换为字符串类型calendar类型

一.个人资料

使用的时间类如下。

System.currentTimeMillis (

System.currentTimeMillis ()生成当前毫秒。 此毫秒是自1970年1月1日0时起的毫秒数,类型为长整型。

日期:

类Date表示特定的瞬间,精确到毫秒。

从JDK 1.1开始,必须使用Calendar类实现日期字段和时间字段之间的转换,并使用DateFormat类设置日期字符串的格式以进行解析。 Date的合适方法已被废除。

Calendar :

Calendar类是一个抽象类,它提供了特定时刻与日历字段(如YEAR、MONTH、DAY_OF_MONTH和HOUR )的转换方式,以及与日历字段(如检索下周日期)的交互方式。

主要使用get方法:

get(intfield )返回给定日历字段的值。 主要参数如下。

HOUR_OF_DAY一天中的某个时间; DAY_OF_WEEK一周的某一天; DAY_OF_MONTH个月的某一天; DAY_OF_YEAR当前年份的天数; WEEK_OF_MONTH当前月的星期几WEEK_OF_YEAR使用当前年的星期几2,1获取当前时间的毫秒值System.currentTimeMillis ()

long time now=system.current time millis (; system.out.println(timenow; date(3358www.Sina.com/

Date date=new Date () getTime ); system.out.println(timenow; 2、获得当前时间System.currentTimeMillis () )不推荐使用)

long time=system.current time millis (; simpledateformatdateformat=newsimpledateformat (yyyy-mm-DD : hh-mm-ss ); string day=date format.format (time; system.out.println(day; 日期

Date date=new Date (; simpledateformatdateformat=newsimpledateformat (yyyy-mm-DD : hh-mm-ss ); string day=date format.format (date; system.out.println(day; 3、获取当前日期(相对于月份)长时间millis=system.current time millis; simpledateformatformat=newsimpledateformat (DD ); string day of month=format.format (time millis ); system.out.println(dayofMonth; Calendar

calendar instance=calendar.getinstance (; intday of month=instance.get (calendar.day _ of _ month ); system.out.println(dayofMonth; 4、用SimpleDateFormat格式化的字符的含义

5、区分YYYY和YYYY格式化日期时,YYYY表示当天所在的年份,大写YYYY是weekinwhichyear(JDK7之后引入的概念),表示当天所在的年份,一周从星期日开始,星期六yyyy

newsimpledateformat (yyyy-mm-ddhh : mm : ss ); 如果当天是2017/12/31,则执行结果为2018/12/31。 (本周跨越2017年和2018年)

三. JDK1.8中的Date/Time API提供了使用Java 8替换java.util.Date和java.util.Calendar的新API。 Date/Time API包含以下几个类:

localdatelocaltimelocaldatetimezoneddatetime 1、LocalDate LocalDate只是一个日期,没有时间。 也就是说,我们只能取得现在的日期,但没有一天的具体时间。

localdatelocaldate=local date.now (; datetimeformatterdatetimeformatter=datetime formatter.of pattern (yyyy-mm-DD ); string date=local date.format (数据格式器; system.out.println(datetime; 2020-01-11的结果:

2020-01-11 2、与LocalDate相反,LocalTime LocalTime只表示一个时间,没有日期。 这意味着只能获取当天的当前时间,而不是实际日期。

local time local time=local time.now (; datetimeformatterdatetimeformatter=datetime formatter.of pattern (hh : mm : ss ); stringtime=local time.format (datetime formatter; system.out.println(time; 结果如下。

1:20336043http://www.Sina.com /

intdaysofthisyear=local date.now ().lengthOfYear );只要本周跨年,返回的YYYY就是下一年。

local date.of (2011,1,1 ).lengthOfYear ); 3、LocalDateTime的最后一个是LocalDateTime,是Java中最常用的Date/Time类,表示前两个类的组合:日期和时间值。

localdatetimelocaldatetime=local datetime.now (datetimeformatterdatetimeformatter=datetime formatter.of pattern (yyy-yyy ) 字符串数据时间=local datetime.format (datetime formatter; system.out.println(datetime; 结果是:

2020-01-11:14:22:37四、简单使用Calendar、获得相关日期元素calendar C1=newgregoriancalendar (; intyear=C1.get(calendar.year ); Calendar常用参数

calendar.year calendar.month calendar.week _ of _ year calendar.week _ of _ month calendar.date calendar.day _ of r endar.hour _ of _ day calendar.minute calendar.day calendar.minute calendar _ offset calendar.field _ count 2,设置日期calend c.set(calendar.year,8000 ); 3、日期计算(日期的加法和减法) Calendar c=new GregorianCalendar ); c.add(calendar.year,-30 ); 4、日历对象和时间对象转换Calendar c=new GregorianCalendar (; c.settime(newdate ) ); 5、String类型为Calendar类型String str='2020-10-10 '; simpledateformatdf=newsimpledateformat (yyyy-mm-DD ); datedate=df.parse(str; Calendar c=new GregorianCalendar (; c .设置时间(date );

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。