java.lang.Object
|--java.util.Date
|--java.sql.Date
|--java.sql.Timestamp
java.lang.Object
|--java.text.Format
|--java.text.DateFormat (抽象类, 不能通过构造函数构造对象, 只可以通过getDateTimeInstance()方法获得该对象)
|--java.text.SimpleDateFormat (具体类)
java.lang.Object
|--java.util.Calendar (抽象类)
|--java.util.GregorianCalendar (具体类)
1.Date
(1)介绍:实际上只是一个包裹类, 它包含的是一个长整型数据, 表示的是从GMT(格林尼治标准时间)1970年, 1月1日00:00:00这一刻之前或者是之后经历的毫秒数. Date类从JDK 1.0就开始进化, 当时它只包含了几个取得或者设置一个日期数据的各个部分的方法, 比如说月, 日, 和年. 这些方法现在遭到了批评并且已经被转移到了Calendar类里去了, 这种改进旨在更好的处理日期数据的国际化格式.
(2)Date最常用的两个方法:java.sql.Date和java.util.Date中的很多方法都已经Deprecated了,不能再用。除了getTime()和setTime()方法。这里要注意与Calendar类的getTime()和setTime()方法的区别。
public long getTime() --> Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
public void setTime(long time) --> Sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT.
2.SimpleDateFormat:用来格式化日期
(1)将Date转换为String (format()方法)
- package edu.hust.test;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Locale ;
- public class DateExample01 {
- public static void main(String[] args) {
-
- Date date = new Date();
-
-
- long lDate = date.getTime();
- System.out.println("当前时间为:" + lDate);
-
-
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd-yyyy, EEEE", Locale.ENGLISH);
- String strDate = simpleDateFormat.format(date);
- System.out.println("用/"MM-dd-yyyy, EEEE/"格式输出当前时间:" + strDate);
-
-
- simpleDateFormat.applyPattern("yyyy年MM月dd日, HH时mm分ss秒");
- strDate = simpleDateFormat.format(date);
- System.out.println("applyPattern()方法改变时间输出格式:" + strDate);
-
-
- }
-
- }
(2)将String(前提:这个String对象必须是日期格式)转换为Date(即将String: 08-08-2008 转换为 long:1199721600000) (parse()方法)
- package edu.hust.test;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Locale;
- public class DateExample02 {
- public static void main(String[] args) {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-DD-yyyy", Locale.CHINESE);
- String strDate = "08-08-2008";
-
- Date date = null;
- try {
- date = simpleDateFormat.parse(strDate);
- } catch (ParseException e) {
- System.out.println("您的日期格式输入有错误, 请按/"MM-DD-yyyy/"格式输入");
- }
-
- long lDate = date.getTime();
-
- System.out.println("将输入日期转换为long型数据, /"08-08-2008/"对应long型数据为:" + lDate);
-
- }
- }
(3)使用DateFormat格式化日期(不常用)
DateFormat可通过getDateTimeInstance()方法获得其对象,DateFormat参数是final常量,系统根据这些常量输出当前时间。注意我们在对getDateTimeInstance的每次调用中都传递了两个值. 第一个参数是日期风格, 而第二个参数是时间风格. 它们都是基本数据类型int(整型). 考虑到可读性, 我们使用了DateFormat类提供的常量: SHORT, MEDIUM, LONG, 和 FULL.
- package edu.hust.test;
- import java.text.DateFormat;
- import java.util.Date;
- public class DateExample02 {
- public static void main(String[] args) {
- Date date = new Date();
- DateFormat shortDateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
- DateFormat mediumDateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
- DateFormat longDateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
- DateFormat fullDateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
- System.out.println(shortDateFormat.format(date));
- System.out.println(mediumDateFormat.format(date));
- System.out.println(longDateFormat.format(date));
- System.out.println(fullDateFormat.format(date));
- }
- }
3.GregorianCalendar:用来设置时间, 可以单独设置yyyy或MM或dd或HH或mm或ss, 或者其任意组合;用来时间的计算.
(1)时间的设置. 注意与Date类的getTime()和setTime()方法的区别。
public final void setTime(Date date) --> Sets this Calendar's time with the given Date.
public final Date getTime() --> Returns a Date object representing this Calendar's time value (millisecond offset from the Epoch").
public void set(int field, int value) --> Sets the given calendar field with the given value.
public int get(int field) --> Returns the value of the given calendar field
public abstract void add(int field, int amount) --> Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules. For example, to subtract 5 days from the current time of the calendar, you can achieve it by calling: add(Calendar.DAY_OF_MONTH, -5).
- package edu.hust.test;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.GregorianCalendar;
- import java.util.Locale;
- public class DateExample03 {
- public static void main(String[] args) {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd-yyyy, EEEE, HH点mm分ss秒", Locale.CHINESE);
-
- GregorianCalendar gregorianCalendar = new GregorianCalendar();
- gregorianCalendar.setTime(new Date());
-
- String strDate = simpleDateFormat.format(gregorianCalendar.getTime());
-
- System.out.println("当前时间为:" + strDate);
-
-
- gregorianCalendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
-
- strDate = simpleDateFormat.format(gregorianCalendar.getTime());
- System.out.println("本周星期五的时间为:" + strDate);
-
- int i = 0;
-
- while (i < 10) {
-
- gregorianCalendar.add(Calendar.DAY_OF_MONTH, 7);
- if (gregorianCalendar.get(Calendar.DAY_OF_MONTH) == 13) {
- strDate = simpleDateFormat.format(gregorianCalendar.getTime());
- System.out.println("从当前时刻起, 星期五与13日重叠的日期有:" + strDate);
- i++;
- }
-
- }
-
-
-
- }
- }
(2)时间的计算:计算出两个时间之间相差几小时、几分钟、几秒钟,怎么做?
方法1
- package edu.hust.test;
- import java.util.GregorianCalendar;
- public class DateExample04 {
- public static void main(String[] args) {
- GregorianCalendar gregorianCalendar_Start = new GregorianCalendar();
- GregorianCalendar gregorianCalendar_End = new GregorianCalendar();
-
- gregorianCalendar_Start.set(2008, 8-1, 8, 20, 8, 8);
- gregorianCalendar_End.set(2008, 8-1, 9, 20, 8, 0);
-
-
-
- long difference = gregorianCalendar_End.getTime().getTime() - gregorianCalendar_Start.getTime().getTime();
-
-
- int difference_Hour = (int) difference/1000/60/60;
- int difference_Minute = (int) difference/1000/60 - difference_Hour * 60;
- int difference_Second = (int) difference/1000 - (difference_Hour * 60 * 60 + difference_Minute * 60);
-
- System.out.println("两个时间点的时间差为:" + difference_Hour + "(小时)" + difference_Minute + "(分钟)" + difference_Second + "(秒)");
- }
-
- }
方法2
- package edu.hust.test;
- import java.util.Calendar;
- import java.util.GregorianCalendar;
- public class DateExample05 {
-
- public static void main(String[] args) {
-
- GregorianCalendar gregorianCalendar_Start = new GregorianCalendar();
- GregorianCalendar gregorianCalendar_End = new GregorianCalendar();
-
- gregorianCalendar_Start.set(2008, 8-1, 8, 20, 8, 8);
- gregorianCalendar_End.set(2009, 8-1, 9, 22, 0, 0);
-
- int difference_Year = gregorianCalendar_End.get(Calendar.YEAR) - gregorianCalendar_Start.get(Calendar.YEAR);
- int difference_Month = gregorianCalendar_End.get(Calendar.MONTH) - gregorianCalendar_Start.get(Calendar.MONTH);
- int difference_Date = gregorianCalendar_End.get(Calendar.DAY_OF_MONTH) - gregorianCalendar_Start.get(Calendar.DAY_OF_MONTH);
- int difference_Hour = gregorianCalendar_End.get(Calendar.HOUR_OF_DAY) - gregorianCalendar_Start.get(Calendar.HOUR_OF_DAY);
- int difference_Minute = gregorianCalendar_End.get(Calendar.MINUTE) - gregorianCalendar_Start.get(Calendar.MINUTE);
- int difference_Second = gregorianCalendar_End.get(Calendar.SECOND) - gregorianCalendar_Start.get(Calendar.SECOND);
-
- System.out.println("两个时间点的'年'差为:" + difference_Year);
- System.out.println("两个时间点的'月'差为:" + difference_Month);
- System.out.println("两个时间点的'日'差为:" + difference_Date);
- System.out.println("两个时间点的'时'差为:" + difference_Hour);
- System.out.println("两个时间点的'分'差为:" + difference_Minute);
- System.out.println("两个时间点的'秒'差为:" + difference_Second);
-
- System.out.println(getIntervalDays(gregorianCalendar_Start, gregorianCalendar_End));
- }
- }
(3)天数差的计算, 把这个单独拿出来是因为应用很广泛.
- package edu.hust.test;
- import java.util.Calendar;
- import java.util.GregorianCalendar;
- public class DateExample05 {
-
- public static void main(String[] args) {
-
- GregorianCalendar gregorianCalendar_Start = new GregorianCalendar();
- GregorianCalendar gregorianCalendar_End = new GregorianCalendar();
-
- gregorianCalendar_Start.set(2008, 8-1, 8, 20, 8, 8);
- gregorianCalendar_End.set(2009, 8-1, 9, 22, 0, 0);
-
- System.out.println(getIntervalDays(gregorianCalendar_Start, gregorianCalendar_End));
- }
-
- public static int getIntervalDays(Calendar calendar_Start, Calendar calendar_End) {
- if (calendar_Start.after(calendar_End)) {
- Calendar temp;
- temp = calendar_Start;
- calendar_Start = calendar_End;
- calendar_End = temp;
- }
- int difference_Day = calendar_End.get(Calendar.DAY_OF_YEAR) - calendar_Start.get(Calendar.DAY_OF_YEAR);
- if (calendar_End.get(Calendar.YEAR) != calendar_Start.get(Calendar.YEAR)) {
- while (calendar_End.get(Calendar.YEAR) != calendar_Start.get(Calendar.YEAR)) {
- difference_Day += calendar_Start.getActualMaximum(Calendar.DAY_OF_YEAR);
- calendar_Start.add(Calendar.YEAR, 1);
- }
- }
- return difference_Day;
- }
-
- }
4.java.sql.Date与java.util.Date
在开发web应用中,针对不同的数据库日期类型,我们需要在我们的程序中对日期类型做各种不同的转换。若对应数据库数据是oracle的Date类型,即只需要年月日的,可以选择使用java.sql.Date类型,若对应的是MSsqlserver 数据库的DateTime类型,即需要年月日时分秒的,选择java.sql.Timestamp类型
(1).java.util.Date与java.sql.Date的相互转换
先讲解一下public int getActualMaximum(int field)方法怎么用.
- package edu.hust.test;
- import java.util.Calendar;
- import java.util.GregorianCalendar;
- public class DateExample06 {
- public static void main(String[] args) {
- GregorianCalendar gregorianCalendar = new GregorianCalendar();
- gregorianCalendar.clear();
-
- gregorianCalendar.set(Calendar.YEAR, 2008);
- gregorianCalendar.set(Calendar.MONTH, 2-1);
-
- int week_Days = gregorianCalendar.getActualMaximum(Calendar.DAY_OF_WEEK);
- int month_Days = gregorianCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
- int year_Days = gregorianCalendar.getActualMaximum(Calendar.DAY_OF_YEAR);
-
-
- System.out.println("当前日期所在周的总天数为:" + week_Days);
- System.out.println("当前日期所在月的总天数为:" + month_Days);
- System.out.println("当前日期所在年的总天数为:" + year_Days);
- }
- }
java.sql.Date和java.util.Date的转换
- package edu.hust.test;
- public class DateExample07 {
- public static void main(String[] args) {
- java.sql.Date sqlDate;
- java.util.Date utilDate;
-
- utilDate = new java.util.Date();
- System.out.println("初始定义utilDate:" + utilDate);
-
- sqlDate = new java.sql.Date(utilDate.getTime());
- System.out.println("转型sqlDate:" + sqlDate);
-
- utilDate = (java.util.Date) sqlDate;
- System.out.println("转型utilDate:" + utilDate);
- }
-
- }
(2)java.sql.Date的值 存入/取出 DB中Date字段中会发生数据截取,为什么?
java.sql.Date是为了配合SQL Date而设置的数据类型。"规范化"的java.sql.Date只包含年月日信息,时分秒毫秒都会清零,格式类似:YYYY-MM-DD。
当我们把一个java.sql.Date值通过PrepareStatement的setDate方法存入数据库时,java程序会对传入的java.sql.Date规范化,非规范化的部分将会被劫取;当我们调用ResultSet的getDate()方法来获得返回值时,java程序会参照"规范"的java.sql.Date来格式化数据库中的数值,非规范化部分将会被劫取。
注:PreparedStament的setDate()的第2参数和ResultSet的getDate()方法的第2个参数都是java.sql.Date