Java获取⽇期时间及其格式处理
对于Java处理⽇期时间有两个类,Date和Calendar,在java8 API⽂档中,Date⼤部分⽅法已被弃⽤,改为⽤Calendar类实现。⼀、DateFormat类
先写⼀下DateFormat类,直接上代码直观(忽略不规范命名,给你⼀个眼神⾃⼰体会)。
public class TestDate {
public static void main(String[] args){
Date date =new Date();
String dateString;
DateFormat dateFormat1 = Instance();//默认SHORT风格
dateString = dateFormat1.format(date);
System.out.println("1: "+ dateString);
DateFormat dateFormat2 = DateInstance();//默认应该是MEDIUM
dateString = dateFormat2.format(date);
System.out.println("2: "+ dateString);
DateFormat dateFormat21 = DateInstance(DateFormat.SHORT);
dateString = dateFormat21.format(date);
System.out.println("21: "+ dateString);
DateFormat dateFormat22 = DateInstance(DateFormat.MEDIUM);
dateString = dateFormat22.format(date);
System.out.println("22: "+ dateString);
DateFormat dateFormat23 = DateInstance(DateFormat.FULL);
dateString = dateFormat23.format(date);
System.out.println("23: "+ dateString);
DateFormat dateFormat3 = TimeInstance();
dateString = dateFormat3.format(date);
System.out.println("3: "+ dateString);
DateFormat dateFormat4 = DateTimeInstance();
dateString = dateFormat4.format(date);
System.out.println("4: "+ dateString);
DateFormat dateFormat41 = DateTimeInstance(DateFormat.FULL, DateFormat.SHORT);
dateString = dateFormat41.format(date);
System.out.println("41: "+ dateString);
DateFormat dateFormat42 = DateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM);
dateString = dateFormat42.format(date);
System.out.println("42: "+ dateString);
DateFormat dateFormat43 = DateTimeInstance(DateFormat.FULL, DateFormat.FULL);
dateString = dateFormat43.format(date);
System.out.println("43: "+ dateString);
DateFormat dateFormat5 = DateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.UK);
dateString = dateFormat5.format(date);
System.out.println("5: "+ dateString);
}
}
输出:
1: 19-8-18 下午4:37
2: 2019-8-18
21: 19-8-18
22: 2019-8-18
23: 2019年8⽉18⽇星期⽇
3: 16:37:34
4: 2019-8-18 16:37:34
java时间日期格式转换41: 2019年8⽉18⽇星期⽇下午4:37
42: 2019年8⽉18⽇星期⽇ 16:37:34
43: 2019年8⽉18⽇星期⽇下午04时37分34秒 CST
5: Sunday, 18 August 2019 16:37:34 o'clock CST
⼆、SimpleDateFormat类
该类允许⽤户⾃定义的⽇期和时间格式。
Date date =new Date();
String dateString;
SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd G hh:mm:ss z"); dateString = format.format(date);
System.out.println(dateString);
输出:
2019-08-18 公元 04:49:12 CST
三、获取当前⽇期
如果只是简单的获取当前时间,Date类完全可以处理。如上栗⼦。
但是如果想获取当前时间的前⼀天等,需要⽤到Calendar类。
Calendar calendar = Instance();
calendar.setTime(new Date());
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); calendar.add(Calendar.DATE,-1);
Date date = Time();
String today = simpleDateFormat.format(date);
System.out.println(today);
输出:
2019-08-17 17:44:03
Calendar类其他⽰例:
Calendar calendar = Instance();
int year = (Calendar.YEAR);
int month = (Calendar.MONTH)+1;//注意⽉份从0开始
int day = (Calendar.DATE);
int hour = (Calendar.HOUR_OF_DAY);
int minute = (Calendar.MINUTE);
int second = (Calendar.SECOND);
System.out.println(year +"-"+ month +"-"+ day +" "+ hour +":"+ minute +":"+ second);输出:
2019-8-18 17:46:59
四、获取指定⽇期为星期⼏
代码如下:
SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse("2022-01-01");
Calendar calendar = Instance();
calendar.setTime(date);
System.out.(Calendar.DAY_OF_WEEK));
注意⼀点,在Java中周⽇到周⼀对应的值依次为1,2,3,4,5,6,7;⽽JavaScript对应的值依次为0,1,2,3,4,5,6.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论