java时间类型处理

  1. package com.copote;
  2. import java.text.DateFormat;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7. import java.util.GregorianCalendar;
  8. public class Test {
  9.     /**
  10.      * @param args
  11.      */
  12.     public static void main(String[] args) {
  13.         // TODO Auto-generated method stub  
  14.         //1
  15.         //定义时间格式,如:200810071500
  16.         SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyyMMddHHmm"); 
  17.         Date date = new Date(); 
  18.          //按照规定格式输出当前时间
  19.         System.out.println(bartDateFormat.format(date)); //200810080913
  20.         
  21.         try {
  22.             //读入时间,转化为时间类型,并且格式化输出
  23.             System.out.println(bartDateFormat.format(bartDateFormat.parse("200810071456"))); // 200810071456
  24.         } catch (ParseException e) {
  25.             // TODO Auto-generated catch block
  26.             e.printStackTrace();
  27.         } 
  28.         
  29.         //2
  30.         GregorianCalendar firstFlight = new GregorianCalendar(1903, Calendar.DECEMBER, 17); 
  31.         Date d = firstFlight.getTime();
  32.         DateFormat df = DateFormat.getDateInstance();
  33.         String s = df.format(d);
  34.         System.out.println("First flight was " + s); //First flight was 1903-12-17
  35.         
  36.         //3 
  37.         GregorianCalendar testFlight1 = new GregorianCalendar(200898917); 
  38.         Date d1 = testFlight1.getTime();
  39.         SimpleDateFormat newDateFormat = new SimpleDateFormat("yyyyMMddHHmm"); 
  40.         newDateFormat.format(d1);
  41.         System.out.println("d1:"+d1);
  42.         System.out.println("First flight was " + newDateFormat.format(d1)); //First flight was 200810080917
  43.         
  44.         
  45.         //4
  46.         testFlight1.add(Calendar.DATE, 8); //200810160917
  47.         testFlight1.add(Calendar.HOUR, 55); //200810181617
  48.         testFlight1.add(Calendar.MINUTE, 55);//200810181712
  49.         Date d4 = testFlight1.getTime();    
  50.         System.out.println("日期加8: " + newDateFormat.format(d4)); //
  51.         System.out.println("或者这一天是星期几:"+testFlight1.get(Calendar.DAY_OF_WEEK));
  52.         System.out.println("DAY_OF_YEAR:"+testFlight1.get(Calendar.DAY_OF_YEAR));
  53.         System.out.println("MONTH:"+testFlight1.get(Calendar.MONTH));
  54.         
  55.         //5 
  56.         GregorianCalendar testFlight5 = new GregorianCalendar(2008931917); 
  57.         testFlight5.add(Calendar.DAY_OF_YEAR, 1);
  58.         Date d5= testFlight5.getTime();
  59.         newDateFormat.format(d5);
  60.         System.out.println("d5:"+d5);
  61.         System.out.println("testFlight5添加一天时间:"+d5);
  62.         
  63.         //6 
  64.         //计算两个日期之间的差值 
  65.         //检查是否隔日运行
  66.         GregorianCalendar testFlight6 = new GregorianCalendar(200898917);       
  67.         Date d6= testFlight6.getTime();
  68.         GregorianCalendar testFlight_6 = new GregorianCalendar(200894); 
  69.         Date d_6= testFlight_6.getTime();
  70.         System.out.println("d6.getTime():"+d6.getTime());
  71.         System.out.println("d_6.getTime():"+d_6.getTime()); 
  72.         long ss=d6.getTime()-d_6.getTime();
  73.         int xs=(int) (ss/(24*60*60*1000)%2);
  74.         System.out.println("xs:"+xs);
  75.         
  76.         //7
  77.         GregorianCalendar calendar=new GregorianCalendar(200898917);
  78.         SimpleDateFormat bartDateFormat_7 = new SimpleDateFormat("yyyyMMddHHmm"); 
  79.         Date date_7 =calendar.getTime(); 
  80.          //按照规定格式输出当前时间
  81.         String sxg=bartDateFormat.format(date);
  82.         System.out.println("将GregorianCalendar类型的时间转化为如同200810080913一样的字符串时间:"+sxg);//200810080913
  83.     
  84.     }
  85.     
  86.     //将GregorianCalendar类型的时间转化为String类型的时间
  87.     String timeConvert(GregorianCalendar calendar){
  88.         String time="";
  89.         //定义时间格式,如:200810071500
  90.         SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyyMMddHHmm"); 
  91.         Date date =calendar.getTime(); 
  92.          //按照规定格式输出当前时间
  93.         time=bartDateFormat.format(date); //200810080913        
  94.         return time;
  95.     }
  96.     //将String类型的时间转化为GregorianCalendar类型的时间
  97.     GregorianCalendar timeConvert(String time){         
  98.         Integer arg0=Integer.valueOf(time.substring(0,4));//年
  99.         Integer arg1=Integer.valueOf(time.substring(4,6))-1;//月 这里月份减一是因为日期形式中月份从0开始
  100.         Integer arg2=Integer.valueOf(time.substring(6,8));//日
  101.         Integer arg3=Integer.valueOf(time.substring(8,10));//时
  102.         Integer arg4=Integer.valueOf(time.substring(10)); //分
  103.         GregorianCalendar calendar=new GregorianCalendar(arg0, arg1, arg2, arg3, arg4);
  104.         return calendar;
  105.     }
  106.     
  107. }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值