《Javadata类PPT课件》由会员分享,可在线阅读,更多相关《Javadata类PPT课件(14页珍藏版)》请在金锄头文库上搜索。
1、日期类Java提供了3个日期类:Date、Calendar和DateFormat。在程序中,对日期的处理主要是如何获取、设置和格式化日期。Java的日期类提供了很多方法以满足程序员的各种需要,请读者参考Java API文档。其中,Date主要用于创建日期对象并获取日期,Calendar可获取和设置日期,DateFormat主要用来对日期格式化,实现各种日期格式串输出。Java语言规定的基准日期为格林威治(GMT)标准时,即1970.1.1 00:00:00。当前日期是由基准日期开始所经历的毫秒数转换出来的。另外,在Java中,为了与数据库SQL操作的日期类型相一致,提供了Date的子类Date
2、,区别是标准日期类在包中,子类在包中,请注意区分。Data类应用Date类获取当前日期,然后按照年月日时分的格式输出import java.util.*;import java.text.*;class Jpro7_7public static void main(Stringargs) Date date=new Date(); SimpleDateFormat sdf= new SimpleDateFormat(yyyy年MM月dd日HH时mm分); System.out.println (sdf.format(date); DateFormat类 DateFormat类的一个目标是建立一
3、个人们能够识别的字符串。你可以使用format()方法转换Date对象为一个字符串。下面的示例程序说明了这个问题: import java.util.*; import java.text.*; public class NowString public static void main(String args) Date now = new Date(); DateFormat df = DateFormat.getDateInstance(); String s = df.format(now); System.out.println(Today is + s); 有时候,拥有原始日期和修
4、改后的日期很重要。不幸的是,你不能简单的创建一个 GregorianCalendar对象,设置它和原来的相等(equal)。原因是两个变量指向同一个Date()对象地址。如果Date对象改变,两个变量就指向改变后的日期对象。代替这种做法,应该创建一个新对象。下面的程序示 import java.util.*; import java.text.*; public class ThreeDates public static void main(String args) GregorianCalendar gc1 = new GregorianCalendar(2000, Calendar.JA
5、NUARY, 1); GregorianCalendar gc2 = gc1; GregorianCalendar gc3 = new GregorianCalendar(2000, Calendar.JANUARY, 1); /Three dates all equal to January 1, 2000 Calendar类gc1.add(Calendar.YEAR, 1); file:/gc1 and gc2 are changed DateFormat df = DateFormat.getDateInstance(); Date d1 = gc1.getTime(); Date d2
6、 = gc2.getTime(); Date d3 = gc3.getTime(); String s1 = df.format(d1); String s2 = df.format(d2); String s3 = df.format(d3); System.out.println(gc1 is + s1); System.out.println(gc2 is + s2); System.out.println(gc3 is + s3); 在下面的程序中,getDates()方法对一个返回日期数组(复习日期)的电子软件很有用。另外,你可以返回单独的一个日期,使用getFirstDay(),g
7、etOneDay(),getOneWeek(),getOnMonth ()和getOneYear().当时间范围超出这个PIM的ReviewDates的计算范围时ReviewDates类演示了怎样计算时间段。现在,你可以容易的修改它用来处理你需要的时间段,象图书馆借书,录影带租赁和抵押计算。首先,ReviewDates类显示在下面: import java.util.*; import java.text.*; public class ReviewDates private GregorianCalendar firstDay, oneDay, oneWeek, oneMonth, oneQ
8、uarter, oneYear; final int dateArraySize = 6; ReviewDates(GregorianCalendar gcDate) int year = gcDate.get(GregorianCalendar.YEAR); int month = gcDate.get(GregorianCalendar.MONTH); int date = gcDate.get(GregorianCalendar.DATE); firstDay = new GregorianCalendar(year, month, date); oneDay = new Gregori
9、anCalendar(year, month, date); oneWeek = new GregorianCalendar(year, month, date); oneMonth = new GregorianCalendar(year, month, date); oneQuarter = new GregorianCalendar(year, month, date); oneYear = new GregorianCalendar(year, month, date); oneDay.add(GregorianCalendar.DATE, 1); oneWeek.add(Gregor
10、ianCalendar.DATE, 7); oneMonth.add(GregorianCalendar.MONTH, 1); oneQuarter.add(GregorianCalendar.MONTH, 3); oneYear.add(GregorianCalendar.YEAR, 1); ReviewDates() this(new GregorianCalendar(); public void listDates() DateFormat df = DateFormat.getDateInstance(DateFormat.LONG); Date startDate = firstD
11、ay.getTime(); Date date1 = oneDay.getTime(); Date date2 = oneWeek.getTime(); Date date3 = oneMonth.getTime(); Date date4 = oneQuarter.getTime(); Date date5 = oneYear.getTime(); String ss = df.format(startDate); String ss1 = df.format(date1); String ss2 = df.format(date2); String ss3 = df.format(date
12、3); String ss4 = df.format(date4); String ss5 = df.format(date5); System.out.println(Start date is + ss); System.out.println(Following review dates are:); System.out.println(ss1); System.out.println(ss2); System.out.println(ss3); System.out.println(ss4); System.out.println(ss5); System.out.println()
13、; public GregorianCalendar getDates() GregorianCalendar memoryDates = new GregorianCalendardateArraySize; memoryDates0 = firstDay; memoryDates1 = oneDay; memoryDates2 = oneWeek; memoryDates3 = oneMonth; memoryDates4 = oneQuarter; memoryDates5 = oneYear; return memoryDates; public GregorianCalendar g
14、etFirstDay() return this.firstDay; public GregorianCalendar getOneDay() return this.oneDay; public GregorianCalendar getOneWeek() return this.oneWeek; public GregorianCalendar getOneMonth() return this.oneMonth; public GregorianCalendar getOneQuarter() return this.oneQuarter; public GregorianCalendar getOneYear() return this.oneYear;