• 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"));
        }
    }
  • 相关阅读:
    git 合并多个commit
    git 修改 Commit Message
    git rebase 命令介绍
    git 忘记切换分支,误将代码commit到了别的分支的解决方法
    会话层的会话和传输层中的连接的区别
    Goland 安装 k8s 源码 的步骤
    Linux export 命令的作用
    Linux 执行脚本时 source 和 . 和 sh 和 ./ 的区别
    the connection to the server 6443 was refused
    Kubernetes 创建 Pod 时,背后到底发生了什么?
  • 原文地址:https://www.cnblogs.com/MyKingDragon/p/9483106.html
Copyright © 2020-2023  润新知