java 方法 时间_java中常用的处理时间方法

package com.sinovatech.infoplat.util; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; /** * 时间工具类 时间格式化
* * @author 樊海潮 * @date 2009-08-19 */ public class DateUtil {     /**      * 日期格式常量 yyyy-MM-dd      */     public static final String DATE_FORMAT = "yyyy-MM-dd";     /**      * 时间格式常量 yyyy-MM-dd HH:mm:ss      */     public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";     /**      * 按照指定的时间格式将传入的时间转换成字符串      *      * @param inputDate      *            时间      * @param inputFormat      *            格式化参数      * @return      */     public static String dateToString(Date inputDate, String inputFormat) {         SimpleDateFormat sdf = new SimpleDateFormat(inputFormat);         return sdf.format(inputDate);     }     /**      * 按照指定的格式将传入的字符串转换成时间      *      * @param inputStr      *            想要转换成时间的字符串      * @param inputFormat      *            时间格式化 年:y 月:M 日:d 时:H 分:m 秒:s      * @return      */     public static Date stringToDate(String inputStr, String inputFormat) {         SimpleDateFormat sdf = new SimpleDateFormat(inputFormat);         Date date = null;         try {             date = sdf.parse(inputStr);         } catch (ParseException e) {             e.printStackTrace();         }         return date;     }     /**      * 获取当前月的最后一天,并格式化为yyyyMMdd000000 用于次月失效的业务      *      * @return      */     public static String getLastDayOfMonth() {         SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");         GregorianCalendar cal = new GregorianCalendar();         Calendar cl = Calendar.getInstance();         int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);         cl.set(Calendar.DAY_OF_MONTH, days);         String timeno = df.format(cl.getTime()) + "000000";         return timeno;     }     /**      * 把当前时间按照指定的格式格式化并返回      *      * @param inputFormat      *            时间格式      * @return      */     public static String getCurrentTime(String inputFormat) {         SimpleDateFormat sdf = new SimpleDateFormat(inputFormat);         return sdf.format(new Date());     }     /**      * 获得星期的名字      *      * @param weekNo      * @return      */     public static String getWeekName(int weekNo) {         String[] weekNames = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };         String weekName = weekNames[(weekNo - 1)];         return weekName;     }     /**      * 比较时间      *      * @param beginDate      * @param toDate      * @return      */     public static boolean compareDate(Date beginDate, Date toDate) {         return (beginDate.before(toDate));     }     /**      *      * @param beforeArg      *            开始时间      * @param afterArg      *            结束时间      * @param formatRegex      *            时间格式化类型 yyyyMMddHHmmss/yyyyMMdd/HHmmss/mmss/等      * @return      */     public static String compareTo(String beforeArg, String afterArg,             String formatRegex) {         String result = "";         SimpleDateFormat format = new SimpleDateFormat(formatRegex);         Date after = null;         Date before = null;         try {             after = format.parse(afterArg);             before = format.parse(beforeArg);         } catch (ParseException e) {             e.printStackTrace();         }         if (after == null || before == null) {             throw new NullPointerException();         }         long tmp = after.getTime() - before.getTime();         System.out.println("tmp == " + tmp);         result = String.valueOf((tmp / 1000 / 60 / 60) + "时" + tmp / 1000 / 60                 % 60 + "分" + tmp / 1000 % 60 + "秒");         return result;     }     public static java.util.Date getTimestamp(String arg) {         Date d = null;         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");         try {             if (arg != null && arg.replaceAll("\\s", "").length() > 0) {                 d = sdf.parse(arg);                 System.out.println(d);             }         } catch (ParseException e) {             e.printStackTrace();         }         return d;     }     public static java.sql.Date getTimes(String arg) {         java.sql.Date sd = null;         java.util.Date d = null;         if (arg != null) {             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");             try {                 d = sdf.parse(arg);                 sd = new java.sql.Date(d.getTime());             } catch (ParseException e) {                 e.printStackTrace();             }         } else {             sd = new java.sql.Date(System.currentTimeMillis());         }         return sd;     }     /**      * 获取前一天的日期      *      * @param m      * @return      */     public static String getChangeDate(Date date, int m) {         SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");         SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");         String nowdate = sdf1.format(date);         Timestamp time = Timestamp.valueOf(nowdate);         long l = time.getTime() + 24 * 60 * 60 * m * 1000;         time.setTime(l);         String changeDate = sdf2.format(time);         return changeDate;     }     public static void main(String[] args) {         boolean bool = false;         Date date = new Date();         SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");         String s = format.format(date);         String s1 = s.substring(0, 8) + "000000";         String s2 = s.substring(0, 8) + "080000";         Date date1 = null;         Date date2 = null;         try {             date1 = format.parse(s1);             date2 = format.parse(s2);         } catch (Exception e) {             e.printStackTrace();         }         if (date.after(date1) && date.before(date2)) {             bool = true;         } else {             bool = false;         }         // System.out.println(dateToString(new Date(), "yyyy-MM-dd HH:mm:ss"));         // System.out.println(new Date() + ":" + new Date());         System.out.println(compareDate(new Date(), new Date()));     } }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值