JAVA基础-日期相关类

来源:互联网 发布:直播黄的软件 编辑:程序博客网 时间:2024/05/16 15:47

1、Date

这里写图片描述
构造函数:
Date() 创建一个Date对象,表示当前这一刻的时间
Date(long date) 创建一个Date对象,表示:从1970年1月1日0时0分0秒 后 过了date毫秒 后的那一刻。
普通方法:
long getTime() 获取的是当前Date对象表示的时间与1970年1月1日0时0分0秒之间的毫秒差
void setTime(long time) 设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。

/*
* 演示:Date类的使用
* 构造函数:
* Date() 创建一个Date对象,表示当前这一刻的时间
* Date(long date) 创建一个Date对象,表示:从1970年1月1日0时0分0秒 后 过了date毫秒 后的那一刻。
* 这里的date必须是long类型,因此,在计算毫秒的时候,最好加上L
* 普通方法:
* long getTime() 获取的是当前Date对象表示的时间与1970年1月1日0时0分0秒之间的毫秒差
* void setTime(long time) 设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。
*
* 需要大家掌握两点:
* Date –> long毫秒值
* long getTime()
* long毫秒值 –> Date
* new Date(long time)
* setTime(long time)
*/
public class DateDemo {
public static void main(String[] args) {
// Date() 创建一个Date对象,表示当前这一刻的时间
Date d1 = new Date();
System.out.println(d1);// Sun Dec 04 10:02:24 CST 2016

    // Date(long date) 创建一个Date对象,表示:从1970年1月1日0时0分0秒 后 过了date毫秒 后的那一刻。    Date d2 = new Date(0);    System.out.println(d2);// Thu Jan 01 08:00:00 CST 1970    // 表示1个小时后    Date d3 = new Date(1000 * 60 * 60);    System.out.println(d3);// Thu Jan 01 09:00:00 CST 1970    // 表示1天后    Date d4 = new Date(1000 * 60 * 60 * 24);    System.out.println(d4);// Fri Jan 02 08:00:00 CST 1970    // 表示1个月后    Date d5 = new Date(1000 * 60 * 60 * 24 * 31L);    System.out.println(1000 * 60 * 60 * 24 * 31);// -1616567296    System.out.println(d5);// Sun Feb 01 08:00:00 CST 1970    // long getTime() 获取的是当前Date对象表示的时间与1970年1月1日0时0分0秒之间的毫秒差    Date d6 = new Date();    System.out.println(d6.getTime());// 1480817632874    System.out.println(System.currentTimeMillis());// 1480817632874    // void setTime(long time) 设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。    // 表示1970的2小时后    // 方式1:构造函数    Date d7 = new Date(1000 * 60 * 60 * 2);    System.out.println(d7);// Thu Jan 01 10:00:00 CST 1970    // 方式2:setTime()    Date d8 = new Date();    d8.setTime(1000 * 60 * 60 * 2);    System.out.println(d8);// Thu Jan 01 10:00:00 CST 1970}

}

2、Dateformat
1.2.1、概述
这里写图片描述
格式化(Date -> String)
String format(Date date) 将一个 Date 格式化为日期/时间字符串
解析(String -> Date)
Date parse(String source)从给定字符串的开始解析文本,以生成一个日期Date
这里写图片描述

构造方法摘要
SimpleDateFormat()用默认的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
SimpleDateFormat(String pattern) 用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。

1.2.2、演示:
这里写图片描述
/*
* 演示:SimpleDateFormat的使用
* 构造方法摘要
* SimpleDateFormat()用默认的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
* SimpleDateFormat(String pattern) 用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
* 格式化(Date -> String)
* String format(Date date) 将一个 Date 格式化为日期/时间字符串
* 解析(String -> Date)
* Date parse(String source)从给定字符串的开始解析文本,以生成一个日期Date
* 注意:在解析的时候,source的格式必须和格式化器的模式匹配!
*/
public class DateFormatDemo {
public static void main(String[] args) throws ParseException {
method_03();
}
// 演示:日期的解析
public static void method_03() throws ParseException {
// 键盘录入用户的生日
Scanner sc = new Scanner(System.in);
System.out.println(“请输入您的生日,格式为:1900-01-01”);
String line = sc.nextLine();
// 创建日期格式化器
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
// 解析
Date birthday = sdf.parse(line);
System.out.println(birthday);// Tue Sep 17 00:00:00 CST 1996
}
// 演示:自定义模式格式化
public static void method_02() {
// SimpleDateFormat(String pattern) 用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat
// 创建日期格式化器
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy年MM月dd日,E HH:mm:ss”);
// 创建日期对象
Date date = new Date();
// 格式化
String str = sdf.format(date);
System.out.println(str);// 2016年12月04日,星期日 10:38:40
}
// 演示:默认模式格式化
public static void method_01() {
// SimpleDateFormat()用默认的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
// 创建日期格式化器
SimpleDateFormat sdf = new SimpleDateFormat();
// 创建日期
Date date = new Date();
// 格式化
String str = sdf.format(date);
System.out.println(str);// 16-12-4 上午10:29
}
}

1.2.3、自定义日期工具类
/**
* 日期工具类,可以实现日期与字符串的格式化和解析
* @author 虎哥
* @version 1.0
*/
public class DateUtils {

/** * 格式化的功能,把Date对象格式化为指定模式的字符串 * @param date 要格式化的日期对象 * @param pattern 要指定的格式化模式 * @return 格式化后的字符串 */public static String format(Date date,String pattern){    // 创建日期格式化器    SimpleDateFormat sdf = new SimpleDateFormat(pattern);    // 格式化    return sdf.format(date);}/** * 格式化功能,把Date对象格式化为默认的模式:1990-01-01 * @param date 要格式化的日期对象 * @return 格式化后的字符串 */public static String format(Date date){    return format(date,"yyyy-MM-dd");}/** * 日期的解析功能 * @param source 要解析的字符串 * @param pattern 解析时的模式 * @return 解析后的Date对象 */public static Date parse(String source,String pattern){    try {        // 创建日期格式化器        SimpleDateFormat sdf = new SimpleDateFormat(pattern);        // 解析        return sdf.parse(source);    } catch (ParseException e) {        throw new RuntimeException("您输入的日期格式不匹配!");    }}/** * 日期的解析功能,默认模式为:1990-01-1 * @param source 要解析的字符串 * @return 解析后的Date对象 */public static Date parse(String source){    return parse(source,"yyyy-MM-dd");}

}

测试:
/*
* 演示:测试日期工具类
*/
public class DateFormatDemo02 {
public static void main(String[] args) {
Date date = new Date();
// 调用日期格式化工具类的功能
String str = DateUtils.format(date);
System.out.println(str);
// 调用日期格式化工具类的功能
String str2 = DateUtils.format(date,”yyyy年MM月dd日”);
System.out.println(str2);

    String source = "1996-09-17";    // 调用日期工具类的解析功能    Date birthday = DateUtils.parse(source);    System.out.println(birthday);// Tue Sep 17 00:00:00 CST 1996}

}

1.2.4、练习:你来到这个世界共多少天?
/*
* 演示:你来到这个世界共多少天?
* 需求:用户键盘录入自己的生日,然后我们计算出他来到世界多少天,打印
* 分析:
* 我们要计算他来到世界多少天,肯定是计算出生日期与当前日期之间的时间差!
* 那么问题来了:用户键盘录入的是字符串,能相减吗?不可以!
* 因此,我们必须把字符串变为Date对象。那么Date对象可以相减吗?
* 我们可以根据Date对象,获取对应的毫秒值。与当前时间的毫秒值相减,就能计算出时间差
* 然后我们再把时间差变成天数就好了!
* 思路:
* A:创建键盘录入的对象
* B:录入用户的生日
* C:把生日解析为Date对象
* D:获取生日的毫秒值
* E:获取当前的毫秒值
* F:求出时间差
* G:转为天数
*/
public class DateFormatDemo03 {
public static void main(String[] args) {
// A:创建键盘录入的对象
Scanner sc = new Scanner(System.in);
// B:录入用户的生日
System.out.println(“请输入您的生日,格式为:1990-01-01”);
String birthday_str = sc.nextLine();
// C:把生日解析为Date对象
Date birthday = DateUtils.parse(birthday_str);
// D:获取生日的毫秒值
long birthday_time = birthday.getTime();
// E:获取当前的毫秒值
long now = System.currentTimeMillis();
// F:求出时间差
long time = now - birthday_time;
// G:转为天数
long day = time / 1000 / 60 / 60 / 24;
System.out.println(“您来到这个世界已经” + day + “天了!”);
}
}

3、Calendar(日历)
1.3.1、概述
这里写图片描述
如何获取日历类的对象?
static Calendar getInstance()使用默认时区和语言环境获得一个日历

1.3.2、获取日历类中的字段值
/*
* 演示:Calendar的获取功能
* 如何获取日历类的对象?
* static Calendar getInstance()使用默认时区和语言环境获得一个日历,表示的是当前时间
* 获取字段值
* int get(int field) 返回给定日历字段的值
*/
public class CalendarDemo01 {
public static void main(String[] args) {
// 创建日历对象
Calendar c = Calendar.getInstance();
// int get(int field) 返回给定日历字段的值
// 获取年
int year = c.get(Calendar.YEAR);
System.out.println(year);// 2016
// 获取月
int month = c.get(Calendar.MONTH);
System.out.println(month);// 0~11
// 获取日
int day = c.get(Calendar.DATE);
System.out.println(day);// 4

    System.out.println(year+"年" + (month + 1) + "月" + day + "日");}

}

1.3.3、修改日历类的字段值
/*
* 演示:修改日历字段的功能
* void set(int field, int value) 将给定的日历字段设置为给定值。
* void set(int year, int month, int date) 设置日历字段 YEAR、MONTH 和 DAY_OF_MONTH 的值。
* void add(int field, int amount) 根据日历的规则,为给定的日历字段添加或减去指定的时间量
*/
public class CalendarDemo02 {
public static void main(String[] args) {
// 获取日历对象
Calendar c = Calendar.getInstance();
printCalendar(c);// 2016年12月4日

    // void set(int field, int value)  将给定的日历字段设置为给定值。     // 需求:我想去到1996

// c.set(Calendar.YEAR, 1996);
// printCalendar(c);
// 需求:我要去到5月
// c.set(Calendar.MONTH, 4);
// printCalendar(c);

    // void set(int year, int month, int date)   设置日历字段 YEAR、MONTH 和 DAY_OF_MONTH 的值。     // 去到:1996-09-17

// c.set(1996, 8, 17);
// printCalendar(c);

    // void add(int field, int amount) 根据日历的规则,为给定的日历字段添加或减去指定的时间量     // 需求:去到10年后

// c.add(Calendar.YEAR, 10);
// printCalendar(c);// 2026年12月4日
// 需求:去到3个月后
// c.add(Calendar.MONTH, 3);
// printCalendar(c);// 2017年3月4日
// 需求:回到10天前
c.add(Calendar.DAY_OF_MONTH, -10);
printCalendar(c);// 2016年11月24日
}

// 封装一个功能,打印Calendar对象的年月日public static void printCalendar(Calendar c) {    int year = c.get(Calendar.YEAR);    int month = c.get(Calendar.MONTH);    int day = c.get(Calendar.DAY_OF_MONTH);    System.out.println(year + "年" + (month + 1) + "月" + day + "日");}

}

1.3.4、练习:计算任意一个年的2月份有几天
/*
* 练习:计算任意一个年的2月份有几天
* 需求:用户任意输入一年,我们判断2月有几天
* 方式1:判断是否是闰年,如果是,那就29天。否则28天
* 方式2:我们把日历调整到用户指定年的3月1日,然后把天字段-1.获取此时的天字段
* 思路:
* A:创建键盘录入对象
* B:录入用户输入的年
* C:创建日历字段
* D:把日历设置到指定年的3月1日
* E:把日历的天字段-1
* F:获取天字段的值,一定是2月的最后一天
*/
public class CalendarDemo03 {
public static void main(String[] args) {
// A:创建键盘录入对象
Scanner sc = new Scanner(System.in);
// B:录入用户输入的年
System.out.println(“请输入任意的年:比如1990”);
String line = sc.nextLine();
int year = Integer.parseInt(line);
// C:创建日历字段
Calendar c = Calendar.getInstance();
// D:把日历设置到指定年的3月1日
c.set(year, 2, 1);
// E:把日历的天字段-1
c.add(Calendar.DAY_OF_MONTH, -1);
// F:获取天字段的值,一定是2月的最后一天
int day = c.get(Calendar.DAY_OF_MONTH);
System.out.println(year + “年的2月分共有” + day + “天”);
sc.close();
}
}