zoukankan      html  css  js  c++  java
  • DateUtil:Java开发中时间转换工具类

    package com.cmbkm.sfywx.webapp.utils;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class DateUtil {
        public static String dateToStr(Date date){
            if(date == null){
                return "";
            }
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return df.format(date);
        }
        
        public static String dateToStr(Date date,String format){
            if(date == null){
                return "";
            }
            SimpleDateFormat df = new SimpleDateFormat(format);
            return df.format(date);
        }
        
        public static Date strToDate(String dateStr){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            try {
                date = df.parse(dateStr);
                
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return date;
        }
        
        public static Date strToDate(String dateStr,String format){
            SimpleDateFormat df = new SimpleDateFormat(format);
            Date date = new Date();
            try {
                date = df.parse(dateStr);
                
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return date;
        }
        
        public static Integer compareTimeOnly(Date date1,Date date2){
            Integer time1 = date1.getSeconds() + date1.getMinutes()*60 + date1.getHours()*60*60;
            Integer time2 = date2.getSeconds() + date2.getMinutes()*60 + date2.getHours()*60*60;
            if(time1 > time2){
                return 1;
            }
            if(time1 < time2){
                return -1;
            }
            return 0;
        }
        
        public static void main(String[] args) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date date1 = df.parse("2016-01-01 10:15:00");
                Date date2 = df.parse("2015-01-01 09:15:00"); 
                System.out.println(DateUtil.compareTimeOnly(date1, date2));
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(DateUtil.dateToStr(new Date(), "HH:mm:ss"));
        }
    }
  • 相关阅读:
    最优贸易 NOIP 2009 提高组 第三题
    Think twice, code once.
    luogu P1378 油滴扩展
    codevs 1002 搭桥
    codevs 1014 装箱问题 2001年NOIP全国联赛普及组
    洛谷P2782 友好城市
    洛谷P1113 杂务
    [HDU1848]Fibonacci again and again
    [POJ2420]A Star not a Tree?
    [SCOI2010]生成字符串
  • 原文地址:https://www.cnblogs.com/MyKingDragon/p/9483106.html
Copyright © 2011-2022 走看看