大家好,今天来为大家分享j *** a时间的一些知识点,和j *** a三个日期类的问题解析,大家要是都明白,那么可以忽略,如果不太清楚的话可以看看本篇文章,相信很大概率可以解决您的问题,接下来我们就一起来看看吧!
本文目录
一、j *** a 实现判断时间早晚(如6:00:00 晚于 4:00:00)
1、写一个比较时间的 *** DateCompare给你,具体看注释,在 *** in *** 里面有测试:
2、importj *** a.text.SimpleDateFor *** t;
3、 publi *** taticvoid *** in(String[]args)throwsException{
4、 Strin *** owTime=newSimpleDateFor *** t("HH:MM").for *** t(newDate());
5、 System.out.println("当前时间为:"+nowTime);
6、 System.out.println("与当日06:00相比");
7、 inti=DateCompare(nowTime,"06:00","HH:MM");
8、 System.out.println("两个时间相等");
9、 System.out.println("当前时间晚于06:00");
10、 System.out.println("当前时间早于06:00");
11、*@paramtype"YYYY-MM-DD""yyyyMMddHH:mm:ss"类型可自定义
12、 publi *** taticintDateCompare(Stringso *** ce,Stringtraget,Stringtype)throwsException{
13、 SimpleDateFor *** tfor *** t=newSimpleDateFor *** t(type);
14、 Dateso *** cedate=for *** t.parse(so *** ce);
15、 Datetragetdate=for *** t.parse(traget);
16、 ret=so *** cedate *** pareTo(tragetdate);
17、}
该 *** 可以转换时间、日期、具体什么格式可以自定义,
18、该 *** 可以转换时间、日期、具体什么格式可以自定义,
19、另外如果你是需要做定时任务,推荐使用quartz有j *** a的实现版。
二、j *** a中date的获取以及使用
1、使用new Date()获取当前日期,new Date().getTime()获取当前毫秒数
2、计算公式,等于获取的当前日期减去或者加上一天的毫秒数。一天的毫秒数的计算公式:24小时*60分钟*60秒*1000毫秒,也是8 *** 00000毫秒。
var preDate= new Date(c *** Date.getTime()- 24*60*60*1000);//前一天
var nextDate= new Date(c *** Date.getTime()+ 24*60*60*1000);//后一天
myDate.getYear();//获取当前年份(2位)
myDate.getFullYear();//获取完整的年份(4位,1970-????)
myDate.getMonth();//获取当前月份(0-11,0 *** 1月)
myDate.getDate();//获取当前日(1-31)
myDate.getDay();//获取当前星期X(0-6,0 *** 星期天)
myDate.getTime();//获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHo *** s();//获取当前小时数(0-23)
myDate.getMinutes();//获取当前分钟数(0-59)
myDate.getSeconds();//获取当前秒数(0-59)
myDate.getMilliseconds();//获取当前毫秒数(0-999)
myDate.toLocaleDateString();//获取当前日期
var mytime=myDate.toLocaleTimeString();//获取当前时间
myDate.toLocaleString();//获取日期与时间
Date.prototype.isLeapYear判断闰年
Date.prototype.For *** t日期格式化
Date.prototype.DateAdd日期计算
Date.prototype.DateDiff比较日期差
Date.prototype.toString日期转字符串
Date.prototype.toArray日期分割为数组
Date.prototype.DatePart取日期的部分信息
Date.prototype.MaxDayOfDate取日期所在月的更大天数
Date.prototype.WeekNumOfYear判断日期所在年的第几周
三、请问J *** A中获取 *** 当前时间该怎么写
*** 一:用j *** a.util.Date类来实现,并结合j *** a.text.DateFor *** t类来实现时间的格式化,看下面代码:
//以下默认时间日期显示方式都是汉语语言方式
//一般语言就默认汉语就可以了,时间日期的格式默认为MEDIUM风格,比如:2008-6-16 20:54:53
//以下显示的日期时间都是再Date类的基础上的来的,还可以利用Calendar类来实现见类TestDate2.j *** a
public static void *** in(String[] args){
Calendar cal= Calendar.getInstance();
DateFor *** t d1= DateFor *** t.getDateInstance();//默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53)
DateFor *** t d2= DateFor *** t.getDateTimeInstance();
DateFor *** t d3= DateFor *** t.getTimeInstance();
DateFor *** t d4= DateFor *** t.getInstance();//使用SHORT风格显示日期和时间
DateFor *** t d5= DateFor *** t.getDateTimeInstance(DateFor *** t.FULL,DateFor *** t.FULL);//显示日期,周,时间(精确到秒)
DateFor *** t d6= DateFor *** t.getDateTimeInstance(DateFor *** t.LONG,DateFor *** t.LONG);//显示日期。时间(精确到秒)
DateFor *** t d7= DateFor *** t.getDateTimeInstance(DateFor *** t.SHORT,DateFor *** t.SHORT);//显示日期,时间(精确到分)
DateFor *** t d8= DateFor *** t.getDateTimeInstance(DateFor *** t.MEDIUM,DateFor *** t.MEDIUM);//显示日期,时间(精确到分)
String str8= d8.for *** t(now);//与SHORT风格相比,这种方式更好用
System.out.println("用Date方式显示时间:"+ now);//此 *** 显示的结果和Calendar.getInstance().getTime()一样
System.out.println("用DateFor *** t.getDateInstance()格式化时间后为:"+ str1);
System.out.println("用DateFor *** t.getDateTimeInstance()格式化时间后为:"+ str2);
System.out.println("用DateFor *** t.getTimeInstance()格式化时间后为:"+ str3);
System.out.println("用DateFor *** t.getInstance()格式化时间后为:"+ str4);
System.out.println("用DateFor *** t.getDateTimeInstance(DateFor *** t.FULL,DateFor *** t.FULL)格式化时间后为:"+ str5);
System.out.println("用DateFor *** t.getDateTimeInstance(DateFor *** t.LONG,DateFor *** t.LONG)格式化时间后为:"+ str6);
System.out.println("用DateFor *** t.getDateTimeInstance(DateFor *** t.SHORT,DateFor *** t.SHORT)格式化时间后为:"+ str7);
System.out.println("用DateFor *** t.getDateTimeInstance(DateFor *** t.MEDIUM,DateFor *** t.MEDIUM)格式化时间后为:"+ str8);
用Date方式显示时间: Mon Jun 16 20:54:53 CST 2008
用DateFor *** t.getDateInstance()格式化时间后为:2008-6-16
用DateFor *** t.getDateTimeInstance()格式化时间后为:2008-6-16 20:54:53
用DateFor *** t.getTimeInstance()格式化时间后为:20:54:53
用DateFor *** t.getInstance()格式化时间后为:08-6-16下午8:54
用DateFor *** t.getDateTimeInstance(DateFor *** t.FULL,DateFor *** t.FULL)格式化时间后为
:2008年6月16日星期一下午08时54分53秒 CST
用DateFor *** t.getDateTimeInstance(DateFor *** t.LONG,DateFor *** t.LONG)格式化时间后为
:2008年6月16日下午08时54分53秒
用DateFor *** t.getDateTimeInstance(DateFor *** t.SHORT,DateFor *** t.SHORT)格式化时间后
用DateFor *** t.getDateTimeInstance(DateFor *** t.MEDIUM,DateFor *** t.MEDIUM)格式化时间
*** 二:用j *** a.util.Calendar类来实现,看下面:
//以下是利用Calendar类来实现日期时间的,和Date类相比较比较简单
public static void *** in(String[] args){
Calendar ca= Calendar.getInstance();
int year= ca.get(Calendar.YEAR);//获取年份
int month=ca.get(Calendar.MONTH);//获取月份
int day=ca.get(Calendar.DATE);//获取日
int minute=ca.get(Calendar.MINUTE);//分
int ho *** =ca.get(Calendar.HOUR);//小时
int second=ca.get(Calendar.SECOND);//秒
int WeekOfYear= ca.get(Calendar.DAY_OF_WEEK);
System.out.println("用Calendar.getInstance().getTime()方式显示时间:"+ ca.getTime());
System.out.println("用Calendar获得日期是:"+ year+"年"+ month+"月"+ day+"日");
System.out.println("用Calendar获得时间是:"+ ho *** +"时"+ minute+"分"+ second+"秒");
System.out.println(WeekOfYear);//显示今天是一周的第几天(我做的这个例子正好是周二,故结果显示2,如果你再周6运行,那么显示6)
用Calendar.getInstance().getTime()方式显示时间: Mon Jun 16 21:54:21 CST 2008
用Calendar获得日期是:2008年5月16日
用Calendar获得时间是:9时54分21秒
总结:中的来说, *** 二是最方便的, *** 一显得分笨拙,不过看个人喜欢了。
j *** a时间的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于j *** a三个日期类、j *** a时间的信息别忘了在本站进行查找哦。