• Java时间工具类汇总(不断累积):java编程时涉及到的时间格式的转换


    package com.shd.biz.appInterface.utils;
    
    import java.sql.Timestamp;
    
    /**
     *  
     *  
     * @function 关于时间戳与时间格式互换的工具方法
     */
    public class DateFormat {
        
        /**
         * 默认日期格式
         */
        private static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
        
        /**
         * 默认构造函数
         */
        public DateFormat(){        
        }
        
        /**
         * @param date 日期
         * @param format 日期格式
         * @return 返回format格式的字符串 
         */
        public String DateToString_Format(Date date,String format){
            if(date != null ){
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                return sdf.format(date);
            }
            return null;
        }
        
        /**
         * @param date 日期
         * @return 返回默认日期格式的字符串 
         */
        public String DateToString_DefaultFormat(Date date){
            if(date == null){
                return null;
            }
            SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_FORMAT);
            return sdf.format(date);
        } 
        
        /**
         * 时间戳转换为字符串
         * @param time
         * @return
         */
        public String timestampToString(Timestamp time) {
            Date date = null;
            if(null != time){
                date = new Date(time.getTime());
            }
            return DateToString_DefaultFormat(date);
        }
        
        /**
         * 时间戳转换为字符串,日期格式:yyyy-MM-dd
         * @param time
         * @return
         */
        public String timestampToString2(Timestamp time) {
            Date date = null;
            if(null != time){
                date = new Date(time.getTime());
            }
            return DateToString_Format(date,"yyyy-MM-dd");
        }
        
        /**
         * @param string 字符串
         * @param format 日期格式
         * @return 日期
         * @throws java.text.ParseException 
         */
        public Date StringToDate(String string, String format){
            if(string == null || "".equals(string)){
                return null;
            }else{
                //...如果没有指定字符串转换的格式,则默认为“yyyy-MM-dd HH:mm:ss”
                if(format ==null || "".equals(format)){
                    format = "yyyy-MM-dd HH:mm:ss";
                }
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                Date date = null;
                try{
                    date = sdf.parse(string);
                    return date;
                }catch(ParseException e){
                    e.printStackTrace();
                }
                return null;
            }    
        }
    }
  • 相关阅读:
    C语言文件路径中的”/“和““
    C语言对文件的操作函数用法详解2
    C语言对文件的操作函数用法详解1
    error MSB8031
    C#操作文件
    C#串口编程
    使用打印方法时,要先引用命名空间: Using System.Drawing.Pringing
    C# 使用printDocument1.Print打印时不显示 正在打印对话框(里面还有一个讨厌的取消按钮)
    在C#里面获得应用程序的当前路径
    Android中的WebView进行直接加载网页(要注意解决权限问题)
  • 原文地址:https://www.cnblogs.com/4AMLJW/p/timeParse202004031408.html
Copyright © 2020-2023  润新知