常用类-日期类

日期类

参考黑马程序员

Date类概述和构造方法

Date代表了一个特定的时间,精确到毫秒

方法名说明
public Date() 分配一个Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒
public Date(long date) 分配一个Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数


Date类的常用方法

方法名说明
public long getTime() 获取的日期对象从1970年1月1日00:00:00到现在的毫秒值
public void setTime(long time) 设置时间,给的是毫秒值

代码

import java.util.Date;

public class DateDemo {
   public static void main(String[] args) {
       //public Date()分配一个Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒
       Date d1 = new Date();
       System.out.println(d1);

       //public Date(long date)分配一个Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数
       long date = 1000 * 60 * 60;
       Date d2 = new Date(date);
       System.out.println(d2);
       System.out.println("--------");

       Date d = new Date();
       //public long getTime()获取的日期对象从1970年1月1日00:00:00到现在的毫秒值
       System.out.println(d.getTime());
       System.out.println(d.getTime() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年");

       //public void setTime(long time)设置时间,给的是毫秒值
       //long time = 1000 * 60 * 60;
       long time = System.currentTimeMillis();
       d.setTime(time);
       System.out.println(d);
  }
}


SimpleDateFormat类概述

SimpleDateFormat是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。我们重点学习日期格式化和解析

日期和时间格式由日期和时间模式字符串指定,在日期和时间模式字符串中,从’A‘到’Z‘以及从’a‘到’z‘引号的字母被解释为表示日期或时间字符串的组件的模式字母

常用的模式字母及对应关系如下:

  • y

  • M 月

  • d 日

  • H 时

  • m 分

  • s 秒



SimpleDateFormat的构造方法

方法名说明
public SimpleDateFormat() 构造一个SimpleDateFormat,使用默认模式和日期格式
public SimpleDateFormat(String pattern) 构造一个SimpleDateFormat使用给定的模式和默认的日期格式


SimpleDateFormat格式化和解析日期

1.格式化(从Date到String)

public final String format(Date date):将日期格式化成日期/时间字符串

2.解析(从String到Date)

public Date parse(String sourse):从给定字符串的开始解析文本以生成日期



代码

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatDemo {
   public static void main(String[] args) throws ParseException {
       //格式化(从Date到String)
       Date d = new Date();
       //SimpleDateFormat sdf = new SimpleDateFormat();//构造一个SimpleDateFormat,使用默认模式和日期格式
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");//构造一个SimpleDateFormat使用给定的模式和默认的日期格式
       String s = sdf.format(d);
       System.out.println(s);
       System.out.println("--------");

       String ss = "2048-08-09 11:11:11";
       //ParseException ss和模式字符串不匹配
       //SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
       SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       Date dd = sdf2.parse(ss);
       System.out.println(dd);
  }
}


Calendar类概述

Calendar为某一时刻和一组日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法

Calendar提供了一个类方法getInstance用于获取Calendar对象,其日历字段已使用当前日期和时间初始化:

Calendar rightNow = Calendar.getInstance();



Calendar的常用方法

方法名说明
public int get(int field) 返回给定日历字段的值
public abstract void add(int field, int amount) 根据日历的规则,将指定的时间量添加或减去给定的日历字段
public final void set(int year,int month,int date) 设置当前日历的年月日


代码

import java.util.Calendar;

public class CalendarDemo {
   public static void main(String[] args) {
       //获取对象
       Calendar c = Calendar.getInstance();//多态的形式
//       System.out.println(c);

       //public int get (int field):返回给定日历字段的值
//       int year = c.get(Calendar.YEAR);
//       int month = c.get(Calendar.MONTH) + 1;//月份从0开始
//       int date = c.get(Calendar.DATE);
//       System.out.println(year + "年" + month + "月" + date + "日");

       //public abstract void add(int field, int amount)根据日历的规则,将指定的时间量添加或减去给定的日历字段
       //三年前的今天
//       c.add(Calendar.YEAR,-3);
//       int year = c.get(Calendar.YEAR);
//       int month = c.get(Calendar.MONTH) + 1;//月份从0开始
//       int date = c.get(Calendar.DATE);
//       System.out.println(year + "年" + month + "月" + date + "日");

       //public final void set(int year,int month,int date)设置当前日历的年月日
       c.set(2048,11,11);
       int year = c.get(Calendar.YEAR);
       int month = c.get(Calendar.MONTH) + 1;//月份从0开始
       int date = c.get(Calendar.DATE);
       System.out.println(year + "年" + month + "月" + date + "日");
  }
}

 

posted @ 2021-03-22 09:58  i爱在西元前  阅读(300)  评论(0)    收藏  举报