zoukankan      html  css  js  c++  java
  • 处理时间工具类小结

    转载请注明出处,谢谢!

    日常代码中,经常会遇到很多对于时间格式的处理业务,最近项目中也设计到些许,记录一下,备用!

    代码如下:

    import java.text.SimpleDateFormat;
    import java.util.Date;
    /**
     * 
     * @author sgl
     * DateFormatUtils 时间格式转换工具
     */
    public class DateFormatUtils {
        /**
         * 年月日  yyyy-MM-dd
         */
        public static final String yyMMdd="yyyy-MM-dd";
        /**
         * 标准类型_汉字版   yyyy年MM月dd日 HH时mm分ss秒
         */
        public static final String NORMAL_CN="yyyy年MM月dd日 HH时mm分ss秒";
        
        /**
         * 标准类型 yyyy-MM-dd HH:mm:ss
         */
        public static final String NORMAL="yyyy-MM-dd HH:mm:ss";
        
        /**
         * 时分秒  HH:mm:ss
         */
        public static final String Hms="HH:mm:ss";
        /**
         * 
         * @param strtime 需要返回的时间格式的正则表达式
         * @return 匹配正则表达式的格式化时间串(yyyy-MM-dd:2015-08-06)返回当前时间
         */
        public static String getCurrTimeStr(String strtime) {
            SimpleDateFormat sdf = new SimpleDateFormat(strtime);
            System.err.println(sdf.format(new Date(System.currentTimeMillis())));
            return sdf.format(new Date(System.currentTimeMillis()));
        }
        /**
         * 
         * @param date 日期
         * @param timeFormat  时间格式
         * @return 返回指定格式的时间字符串
         */
        public static String getTimeStrByDate(Date date,String timeFormat)
        {
            SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
            return sdf.format(date);
        }
        
        /**
         * 将特定格式的时间串转换成Date类型(其中年份减去1900,另有意义,此处不做说明,网上相关资料很多)
         * @param times 格式如2016-3-22类型的时间字符串
         * @return
         */
        public static Date getDates(String times)
        {
            String[] temps=times.split("-");
            int[] dates=new int[temps.length];
            for (int i = 0; i < temps.length; i++) {
                dates[i]=Integer.parseInt(temps[i]);
            }
            return new Date(dates[0]-1900,dates[1],dates[2]);
        }
    }
  • 相关阅读:
    越长大越孤单
    关于ASP.NET 启动Process的讨论
    利用selenium开发一个功能测试的框架
    开博啦(上班时间)
    利用回发 实现一个简单的AutoComplete功能
    FIFO和双端口RAM
    8位定点数开方程序(贴下来以后研究)
    Eclipse下文件读取的问题:Failed to reading file xxxxx
    Error:NgdBuild:604解决方法(添加NGC文件方法)
    Mandelbrot:美丽的分形
  • 原文地址:https://www.cnblogs.com/myadmin/p/5309965.html
Copyright © 2011-2022 走看看