首页 > 编程知识 正文

java判断日期,java 当前日期

时间:2023-05-04 09:54:24 阅读:149687 作者:3725

java获取当前的日期和时间

javaprovidestwoclassestogetcurrentdateandtimedateandcalendar。

Java包含两个类:-Date和Calendar,用于获取当前日期和时间。

在Java中获取当前日期hereisthesimpleprogramshowinghowwecanusetheseclassestogetthecurrentdateandtimeinjava。

这是一个简单的程序,演示如何使用这些类获取Java的当前日期和时间。

package com.journaldev.util; import java.util.Calendar; import java.util.Date; publicclassdateandtimeutil { publicstaticvoidmain (string [ ] args )/getcurrentdateusingdatedate=new date }; system.out.println (' currentdateusingdate=' date.tostring ) ); //getcurrentdateusingcalendarcalendarcal=calendar.getinstance (; system.out.println (' currentdateusingcalendar=' cal.gettime ) ); //getcurrenttimeinmillisecondssystem.out.println (' currenttimeinmillisecondsusingdate=' date.gettime ) ); system.out.println (currenttimeinmillisecondsusingcalendar=' cal.gettimeinmillis () ); } outputoftheaboveprogramis :

上面程序的输出如下。

currentdateusingdate=thu nov 151:51336003 PST 2012 currentdateusingcalendar=thu nov 1513:51336003 PST 2012 currentimeimmmin currenttimeinmillisecondsusingcalendar=1353016263711 fewpointstonotewhilegettingthecurrentdateite

在Java中获取当前日期时应注意的事项:

calendar.getinstance (ismoreexpensivemethod,souseitonlywhenyouneedcalendarobject.elseusedateobject.calendar.get indat thenumberofmillisecondsisthenumberofmillisecondssincejanuary 1,1970,00336000336000 GMT。

毫秒数是自格林尼治标准时间1970年1月1日00:00:00以来的毫秒数。 noticethedifferencebetweenmillisecondsincalendaranddateobject,it’sbecauseofthetimetakenforjavaprogramtocreatedateandateandandcaled cand

请注意Calendar和Date对象之间的毫秒差异,因为Java程序创建Date和Calendar对象所用的时间。 因为首先要创建日期对象,所以毫秒值较低。 youcanusesimpledateformatclasstoformatthedateindifferentways。

您可以使用SimpleDateFormat类以多种方式设置日期的格式。

3358 www.Sina.com/: ifyouareworkingonjava 8,thenyoushouldconsiderusingnewdatetimeapi.for more details,pleasereadjavils

33558 www.Sina.com/:如果使用的是Java 8,则必须考虑使用新的Date Time API。 详细内容请阅读Java Date Time API Tutorial。

翻译自:https://www.journal dev.com/703/get-current-date-time-Java

java获取当前的日期和时间

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