zoukankan      html  css  js  c++  java
  • java 时间操作

     1     /**
     2      * 时间比较
     3      * @param args
     4      */
     5     public static void main(String[] args) {
     6         DateFormat df = new SimpleDateFormat("HH:mm:ss");//创建日期转换对象HH:mm:ss为时分秒,年月日为yyyy-MM-dd
     7         try {
     8             Date dt1 = df.parse("15:00:00");//将字符串转换为date类型
     9             Date dt2 = df.parse("17:00:00");
    10             if(dt1.getTime()>dt2.getTime())//比较时间大小,如果dt1大于dt2
    11             {
    12                 System.out.println("yes");
    13             }
    14             else
    15             {
    16                 System.out.println("no");//运行输出no
    17             }
    18         } catch (ParseException e) {
    19             e.printStackTrace();
    20         }
    21 
    22     }
     1 /**
     2      * 给时间加上几个小时
     3      * @param day 当前时间 格式:yyyy-MM-dd HH:mm:ss
     4      * @param hour 需要加的时间
     5      * @return
     6      */
     7     public static String addDateMinut(String day, int hour){   
     8         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     9         Date date = null;   
    10         try {   
    11             date = format.parse(day);   
    12         } catch (Exception ex) {   
    13             ex.printStackTrace();   
    14         }   
    15         if (date == null)   
    16             return "";   
    17         System.out.println("front:" + format.format(date)); //显示输入的日期  
    18         Calendar cal = Calendar.getInstance();   
    19         cal.setTime(date);   
    20         cal.add(Calendar.HOUR, hour);// 24小时制   
    21         date = cal.getTime();   
    22         System.out.println("after:" + format.format(date));  //显示更新后的日期 
    23         cal = null;   
    24         return format.format(date);   
    25 
    26     }
     1 package com.test.jedis;
     2 
     3 import java.text.ParseException;
     4 import java.text.SimpleDateFormat;
     5 import java.util.Calendar;
     6 import java.util.Date;
     7 
     8 public class Test {
     9     
    10     public static void main(String[] args) throws ParseException {
    11         plusDay2(-1);
    12     }
    13 
    14 
    15 /**
    16      * 指定日期加上天数后的日期
    17      * @param num 为增加的天数
    18      * @param newDate 创建时间
    19      * @return
    20      * @throws ParseException 
    21      */
    22     public static String plusDay(int num,String newDate) throws ParseException{
    23         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    24         Date  currdate = format.parse(newDate);
    25         System.out.println("现在的日期是:" + currdate);
    26         Calendar ca = Calendar.getInstance();
    27         ca.add(Calendar.DATE, num);// num为增加的天数,可以改变的
    28         currdate = ca.getTime();
    29         String enddate = format.format(currdate);
    30         System.out.println("增加天数以后的日期:" + enddate);
    31         return enddate;
    32     }
    33     
    34     
    35     //当前日期加上天数:
    36 
    37 
    38 /**
    39      * 当前日期加上天数后的日期
    40      * @param num 为增加的天数
    41      * @return
    42      */
    43     public static String plusDay2(int num){
    44         Date d = new Date();
    45         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    46         String currdate = format.format(d);
    47         System.out.println("现在的日期是:" + currdate);
    48 
    49         Calendar ca = Calendar.getInstance();
    50         ca.add(Calendar.DATE, num);// num为增加的天数,可以改变的
    51         d = ca.getTime();
    52         String enddate = format.format(d);
    53         System.out.println("增加天数以后的日期:" + enddate);
    54         return enddate;
    55     }
    56 
    57 
    58 
    59 }
    

      

  • 相关阅读:
    几何画板表现两集合的差集的教程
    MathType如何编辑大三角形符号
    几何画板如何绘制动态正切函数图像
    MathType如何设置标尺的单位
    模拟按键
    oauth2.0
    PHP CURL POST提交
    Eclipse导入到web项目没有run on server
    实时刷新
    js 实时数据显示
  • 原文地址:https://www.cnblogs.com/chancy/p/8527244.html
Copyright © 2011-2022 走看看