java基础进阶-09-日期类

日期类

Date

Date d1 = new Date();

long date = 1000*60*60;
Date d2 = new Date(date)//1970-1-1 0:0:0 以后
  • getTime
  • setTime(long time)
Date d = new Date();
System.out.println(d.getTime());

long time = System.currentTimeMillis();
d.setTime(time);

SimpleDateFormat格式化

  • SimpleDateFormat()
  • SimpleDateFormat(String pattern)
  • String format(Date date)
  • Date pase(String soruce)
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat();
String s = sdf.format(d);
System.out.println(s);

SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s2 = sdf22.format(d);
System.out.println(s2);
String ss = "2000-01-01 11:11:11"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf.parse(ss);
System.out.println(d);

日期工具类

  • 自制
public class DateUtils{
    
    public static String dataToString(Date date,String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String s = sdf.format(date);
        return s;
    }
    
    public static Date stringToDate(String s,String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = sdf.parse(s);
        return date;
    }
}

Calendar

Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int year = c.get(Calendar.DATE);

  • get(int field)
  • add(int field,int amount)
  • set(int year,int mounth,int date)
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
c.add(Calendar.YEAR,-3)//减去3年
int year2 = c.get(Calendar.YEAR);
Calendar c = Calendar.getInstance();
c.set(2048,11,11);//2048-12-11
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int year = c.get(Calendar.DATE);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值