- package com.copote;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.GregorianCalendar;
- public class Test {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //1
- //定义时间格式,如:200810071500
- SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyyMMddHHmm");
- Date date = new Date();
- //按照规定格式输出当前时间
- System.out.println(bartDateFormat.format(date)); //200810080913
- try {
- //读入时间,转化为时间类型,并且格式化输出
- System.out.println(bartDateFormat.format(bartDateFormat.parse("200810071456"))); // 200810071456
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //2
- GregorianCalendar firstFlight = new GregorianCalendar(1903, Calendar.DECEMBER, 17);
- Date d = firstFlight.getTime();
- DateFormat df = DateFormat.getDateInstance();
- String s = df.format(d);
- System.out.println("First flight was " + s); //First flight was 1903-12-17
- //3
- GregorianCalendar testFlight1 = new GregorianCalendar(2008, 9, 8, 9, 17);
- Date d1 = testFlight1.getTime();
- SimpleDateFormat newDateFormat = new SimpleDateFormat("yyyyMMddHHmm");
- newDateFormat.format(d1);
- System.out.println("d1:"+d1);
- System.out.println("First flight was " + newDateFormat.format(d1)); //First flight was 200810080917
- //4
- testFlight1.add(Calendar.DATE, 8); //200810160917
- testFlight1.add(Calendar.HOUR, 55); //200810181617
- testFlight1.add(Calendar.MINUTE, 55);//200810181712
- Date d4 = testFlight1.getTime();
- System.out.println("日期加8: " + newDateFormat.format(d4)); //
- System.out.println("或者这一天是星期几:"+testFlight1.get(Calendar.DAY_OF_WEEK));
- System.out.println("DAY_OF_YEAR:"+testFlight1.get(Calendar.DAY_OF_YEAR));
- System.out.println("MONTH:"+testFlight1.get(Calendar.MONTH));
- //5
- GregorianCalendar testFlight5 = new GregorianCalendar(2008, 9, 31, 9, 17);
- testFlight5.add(Calendar.DAY_OF_YEAR, 1);
- Date d5= testFlight5.getTime();
- newDateFormat.format(d5);
- System.out.println("d5:"+d5);
- System.out.println("testFlight5添加一天时间:"+d5);
- //6
- //计算两个日期之间的差值
- //检查是否隔日运行
- GregorianCalendar testFlight6 = new GregorianCalendar(2008, 9, 8, 9, 17);
- Date d6= testFlight6.getTime();
- GregorianCalendar testFlight_6 = new GregorianCalendar(2008, 9, 4);
- Date d_6= testFlight_6.getTime();
- System.out.println("d6.getTime():"+d6.getTime());
- System.out.println("d_6.getTime():"+d_6.getTime());
- long ss=d6.getTime()-d_6.getTime();
- int xs=(int) (ss/(24*60*60*1000)%2);
- System.out.println("xs:"+xs);
- //7
- GregorianCalendar calendar=new GregorianCalendar(2008, 9, 8, 9, 17);
- SimpleDateFormat bartDateFormat_7 = new SimpleDateFormat("yyyyMMddHHmm");
- Date date_7 =calendar.getTime();
- //按照规定格式输出当前时间
- String sxg=bartDateFormat.format(date);
- System.out.println("将GregorianCalendar类型的时间转化为如同200810080913一样的字符串时间:"+sxg);//200810080913
- }
- //将GregorianCalendar类型的时间转化为String类型的时间
- String timeConvert(GregorianCalendar calendar){
- String time="";
- //定义时间格式,如:200810071500
- SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyyMMddHHmm");
- Date date =calendar.getTime();
- //按照规定格式输出当前时间
- time=bartDateFormat.format(date); //200810080913
- return time;
- }
- //将String类型的时间转化为GregorianCalendar类型的时间
- GregorianCalendar timeConvert(String time){
- Integer arg0=Integer.valueOf(time.substring(0,4));//年
- Integer arg1=Integer.valueOf(time.substring(4,6))-1;//月 这里月份减一是因为日期形式中月份从0开始
- Integer arg2=Integer.valueOf(time.substring(6,8));//日
- Integer arg3=Integer.valueOf(time.substring(8,10));//时
- Integer arg4=Integer.valueOf(time.substring(10)); //分
- GregorianCalendar calendar=new GregorianCalendar(arg0, arg1, arg2, arg3, arg4);
- return calendar;
- }
- }