java calendar formate_java常用类:Date/Calendar/SimpleDateFormat

Date

Date表示特定的瞬间,精确到毫秒。Date类中的大部分方法都已经被Calendar类中的方法取代

时间单位:

1秒=1000毫秒

1毫秒=1000微秒

1微秒=1000纳秒

import java.util.Date;

public class Demo01 {

public static void main(String[] args) {

// 1 创建Date对象

Date date1 = new Date();

System.out.println(date1.toString());

System.out.println(date1.toLocaleString());//过时了

// 创建昨天的

Date date2 = new Date(date1.getTime() - (60*60*24*1000));

System.out.println(date2.toLocaleString());

// 2 方法after before

boolean b1 = date1.after(date2);

System.out.println(b1);

boolean b2 = date1.before(date2);

System.out.println(b2);

// 比较compareTo();

int d = date1.compareTo(date2);

System.out.println(d);

// 比较是否相等 equals()

boolean b3 = date1.equals(date2);

System.out.println(b3);

}

}

//结果:

//Sat Dec 26 21:48:30 GMT+08:00 2020

//2020-12-26 21:48:30

//2020-12-25 21:48:30

//true

//false

//1

//false

Calendar

Calendar提供了获取或设置各种日历字段的方法

构造方法

protected Calendar():由于修饰符是protected,所以无法直接创建该对象

其他方法

方法名

说明

static Calendar getInstance()

使用默认时区和区域获取日历

void set(int year, int month, int date, int hourofday, int minute, int second)

设置日历的年、月、日、时、分、秒

int get(int field)

返回给定日历字段的值。字段比如年、月、日

void setTime(Date date)

用给定的date设置此日历时间

Date getTime()

返回一个date表示此日历的时间

void add(int field, int amount)

按照日历的规则,给指定字段添加或减少时间量

long getTimeInMilles()

毫秒为单位返回该日历的时间值

public class Demo01 {

public static void main(String[] args) {

//1.创建Calendar对象

Calendar calender=Calendar.getInstance();

System.out.println(calender.getTime().toLocaleString());

System.out.println(calender.getTimeInMillis());

//2获取时间信息

int year=calender.get(Calendar.YEAR);

//月是从0到11

int month=calender.get(Calendar.MONTH);

int day=calender.get(Calendar.DAY_OF_MONTH);//Date

int hour=calender.get(Calendar.HOUR_OF_DAY);//Hour 12小时

int minute=calender.get(Calendar.MINUTE);

int second=calender.get(Calendar.SECOND);

System.out.println(year+"年"+(month+1)+"月"+day+"日"+hour+":"+minute+":"+second);

//3修改时间

Calendar calendar2=Calendar.getInstance();

System.out.println(calendar2.getTime().toLocaleString());

calendar2.set(Calendar.DAY_OF_MONTH,5);//修改day

System.out.println(calendar2.getTime().toLocaleString());

//4使用add方法修改时间

calendar2.add(Calendar.HOUR,1);

System.out.println(calendar2.getTime().toLocaleString());

//5 获取当前时间的最大值或者最小值

System.out.println(calendar2.getActualMaximum(Calendar.DAY_OF_MONTH));//最大值

}

}

//结果:

//2020-12-27 10:23:03

//1609035783235

//2020年12月27日10:23:3

//2020-12-27 10:23:03

//2020-12-5 10:23:03

//2020-12-5 11:23:03

//31

SimpleDateFormat

SimpleDateFormat是一个以与语言环境有关的方式来格式化和解析日期的具体类

进行格式化(日期-->文本)、解析(文本-->日期)

常用的时间模式字母

字母

日期或时间

示例

y

2019

M

年中月份

08

d

月中天数

10

H

一天中小时(0-23)

22

m

分钟

16

s

59

S

毫秒

367

public class Demo01 {

public static void main(String[] args) throws Exception{

//1.创建SimpleDateFormat对象

SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//字符串参数就是日期的模式

//2创建Date

Date date=new Date();

//格式化date 把date转成字符串

String str=sdf.format(date);

System.out.println(str);

//解析 把字符串转成date

Date date2 = sdf.parse("2020/05/01 12:22:00");

System.out.println(date2);

}

}

//结果:

//2020/12/27 10:40:15

//Fri May 01 12:22:00 GMT+08:00 2020

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值