Date代表了一个特定的时间,精确到毫秒
| 方法名 | 说明 |
|---|---|
| public Date() | 分配一个Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒 |
| public Date(long date) | 分配一个Date对象,并将其初始化为表示从标准基准时间(1970年1月1日00:00:00)起指定的毫秒数 |
| 方法名 | 说明 |
|---|---|
| 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是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。我们重点学习日期格式化和解析
日期和时间格式由日期和时间模式字符串指定,在日期和时间模式字符串中,从’A‘到’Z‘以及从’a‘到’z‘引号的字母被解释为表示日期或时间字符串的组件的模式字母
常用的模式字母及对应关系如下:
| 方法名 | 说明 |
|---|---|
| public SimpleDateFormat() | 构造一个SimpleDateFormat,使用默认模式和日期格式 |
| public SimpleDateFormat(String pattern) | 构造一个SimpleDateFormat使用给定的模式和默认的日期格式 |
public final String format(Date 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提供了一个类方法getInstance用于获取Calendar对象,其日历字段已使用当前日期和时间初始化:
Calendar rightNow = Calendar.getInstance();
| 方法名 | 说明 |
|---|---|
| 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 + "日");
}
}