使用java中的Date和Calendar类
Java 语言的Calendar(日历),Date(日期), 和DateFormat(日期格式)组成了Java标准的一个基本但是非常重要的部分. 日期是商业逻辑计算一个关键的部分. 所有的开发者都应该能够计算未来的日期, 定制日期的显示格式, 并将文本数据解析成日期对象. 我们写了两篇文章, 这是第一篇, 我们将大概的学习日期, 日期格式, 日期的解析和日期的计算.
我们将讨论下面的类:
1、具体类(和抽象类相对)java.util.Date
2、抽象类java.text.DateFormat 和它的一个具体子类,java.text.SimpleDateFormat
3、抽象类java.util.Calendar 和它的一个具体子类,java.util.GregorianCalendar
具体类可以被实例化, 但是抽象类却不能. 你首先必须实现抽象类的一个具体子类.
Date 类从Java 开发包(JDK) 1.0 就开始进化, 当时它只包含了几个取得或者设置一个日期数据的各个部分的方法, 比如说月, 日, 和年. 这些方法现在遭到了批评并且已经被转移到了Calendar类里去了, 我们将在本文中进一步讨论它. 这种改进旨在更好的处理日期数据的国际化格式. 就象在JDK 1.1中一样, Date 类实际上只是一个包裹类, 它包含的是一个长整型数据, 表示的是从GMT(格林尼治标准时间)1970年, 1 月 1日00:00:00这一刻之前或者是之后经历的毫秒数.
一、创建一个日期对象
让我们看一个使用系统的当前日期和时间创建一个日期对象并返回一个长整数的简单例子. 这个时间通常被称为Java 虚拟机(JVM)主机环境的系统时间.
import java.util.Date;public class DateExample1 {public static void main(String[] args) {// Get the system date/timeDate date = new Date();System.out.println(date.getTime());}}
import java.text.SimpleDateFormat;import java.util.Date;public class DateExample2 {public static void main(String[] args) {SimpleDateFormat bartDateFormat =new SimpleDateFormat("EEEE-MMMM-dd-yyyy");Date date = new Date();System.out.println(bartDateFormat.format(date));}}
import java.text.SimpleDateFormat;import java.util.Date;public class DateExample3 {public static void main(String[] args) {// Create a date formatter that can parse dates of// the form MM-dd-yyyy.SimpleDateFormat bartDateFormat =new SimpleDateFormat("MM-dd-yyyy");// Create a string containing a text date to be parsed.String dateStringToParse = "9-29-2001";try {// Parse the text version of the date.// We have to perform the parse method in a// try-catch construct in case dateStringToParse// does not contain a date in the format we are expecting.Date date = bartDateFormat.parse(dateStringToParse);// Now send the parsed date as a long value// to the system output.System.out.println(date.getTime());}catch (Exception ex) {System.out.println(ex.getMessage());}}}
import java.text.DateFormat;import java.util.Date;public class DateExample4 {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));}}
import java.util.GregorianCalendar;import java.util.Date;import java.text.DateFormat;public class DateExample5 {public static void main(String[] args) {DateFormat dateFormat =DateFormat.getDateInstance(DateFormat.FULL);// Create our Gregorian Calendar.GregorianCalendar cal = new GregorianCalendar();// Set the date and time of our calendar// to the system's date and timecal.setTime(new Date());System.out.println("System Date: " +dateFormat.format(cal.getTime()));// Set the day of week to FRIDAYcal.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.FRIDAY);System.out.println("After Setting Day of Week to Friday: " +dateFormat.format(cal.getTime()));int friday13Counter = 0;while (friday13Counter <= 10) {// Go to the next Friday by adding 7 days.cal.add(GregorianCalendar.DAY_OF_MONTH, 7);// If the day of month is 13 we have// another Friday the 13th.if (cal.get(GregorianCalendar.DAY_OF_MONTH) == 13) {friday13Counter++;System.out.println(dateFormat.format(cal.getTime()));}}}}