zoukankan      html  css  js  c++  java
  • java日期格式的常用操作

    顾晓北 | 大侠五级 |园豆:9353 | 2016-08-04 16:17
     

    其他回答(1)
    0
      1 public class DateUtils extends PropertyEditorSupport {
      2     // 各种时间格式
      3     public static final SimpleDateFormat date_sdf = new SimpleDateFormat(
      4             "yyyy-MM-dd");
      5     // 各种时间格式
      6     public static final SimpleDateFormat yyyyMMdd = new SimpleDateFormat(
      7             "yyyyMMdd");
      8     // 各种时间格式
      9     public static final SimpleDateFormat date_sdf_wz = new SimpleDateFormat(
     10             "yyyy年MM月dd日");
     11     public static final SimpleDateFormat time_sdf = new SimpleDateFormat(
     12             "yyyy-MM-dd HH:mm");
     13     public static final SimpleDateFormat yyyymmddhhmmss = new SimpleDateFormat(
     14     "yyyyMMddHHmmss");
     15     public static final SimpleDateFormat short_time_sdf = new SimpleDateFormat(
     16             "HH:mm");
     17     public static final  SimpleDateFormat datetimeFormat = new SimpleDateFormat(
     18     "yyyy-MM-dd HH:mm:ss");
     19     // 以毫秒表示的时间
     20     private static final long DAY_IN_MILLIS = 24 * 3600 * 1000;
     21     private static final long HOUR_IN_MILLIS = 3600 * 1000;
     22     private static final long MINUTE_IN_MILLIS = 60 * 1000;
     23     private static final long SECOND_IN_MILLIS = 1000;
     24     // 指定模式的时间格式
     25     private static SimpleDateFormat getSDFormat(String pattern) {
     26         return new SimpleDateFormat(pattern);
     27     }
     28 
     29     /**
     30      * 当前日历,这里用中国时间表示
     31      * 
     32      * @return 以当地时区表示的系统当前日历
     33      */
     34     public static Calendar getCalendar() {
     35         return Calendar.getInstance();
     36     }
     37 
     38     /**
     39      * 指定毫秒数表示的日历
     40      * 
     41      * @param millis
     42      *            毫秒数
     43      * @return 指定毫秒数表示的日历
     44      */
     45     public static Calendar getCalendar(long millis) {
     46         Calendar cal = Calendar.getInstance();
     47         cal.setTime(new Date(millis));
     48         return cal;
     49     }
     50     /**
     51      * 时间转字符串
     52      * @return
     53      */
     54     public static String date2SStr() 
     55     {
     56         Date date=getDate();
     57         if (null == date) {
     58             return null;
     59         }
     60         return yyyyMMdd.format(date);
     61     }
     62 
     63     // ////////////////////////////////////////////////////////////////////////////
     64     // getDate
     65     // 各种方式获取的Date
     66     // ////////////////////////////////////////////////////////////////////////////
     67 
     68     /**
     69      * 当前日期
     70      * 
     71      * @return 系统当前时间
     72      */
     73     public static Date getDate() {
     74         return new Date();
     75     }
     76 
     77     /**
     78      * 指定毫秒数表示的日期
     79      * 
     80      * @param millis
     81      *            毫秒数
     82      * @return 指定毫秒数表示的日期
     83      */
     84     public static Date getDate(long millis) {
     85         return new Date(millis);
     86     }
     87 
     88     /**
     89      * 时间戳转换为字符串
     90      * 
     91      * @param time
     92      * @return
     93      */
     94     public static String timestamptoStr(Timestamp time) {
     95         Date date = null;
     96         if (null != time) {
     97             date = new Date(time.getTime());
     98         }
     99         return date2Str(date_sdf);
    100     }
    101 
    102     /**
    103      * 字符串转换时间戳
    104      * 
    105      * @param str
    106      * @return
    107      */
    108     public static Timestamp str2Timestamp(String str) {
    109         Date date = str2Date(str, date_sdf);
    110         return new Timestamp(date.getTime());
    111     }
    112     /**
    113      * 字符串转换成日期
    114      * @param str
    115      * @param sdf
    116      * @return
    117      */
    118     public static Date str2Date(String str, SimpleDateFormat sdf) {
    119         if (null == str || "".equals(str)) {
    120             return null;
    121         }
    122         Date date = null;
    123         try {
    124             date = sdf.parse(str);
    125             return date;
    126         } catch (ParseException e) {
    127             e.printStackTrace();
    128         }
    129         return null;
    130     }
    131 
    132     /**
    133      * 日期转换为字符串
    134      * 
    135      * @param date
    136      *            日期
    137      * @param format
    138      *            日期格式
    139      * @return 字符串
    140      */
    141     public static String date2Str(SimpleDateFormat date_sdf) {
    142         Date date=getDate();
    143         if (null == date) {
    144             return null;
    145         }
    146         return date_sdf.format(date);
    147     }
    148     /**
    149      * 格式化时间
    150      * @param data
    151      * @param format
    152      * @return
    153      */
    154     public static String dataformat(String data,String format)
    155     {
    156         SimpleDateFormat sformat = new SimpleDateFormat(format);
    157         Date date=null;
    158         try {
    159              date=sformat.parse(data);
    160         } catch (ParseException e) {
    161             // TODO Auto-generated catch block
    162             e.printStackTrace();
    163         }
    164         return sformat.format(date);
    165     }
    166     /**
    167      * 日期转换为字符串
    168      * 
    169      * @param date
    170      *            日期
    171      * @param format
    172      *            日期格式
    173      * @return 字符串
    174      */
    175     public static String date2Str(Date date, SimpleDateFormat date_sdf) {
    176         if (null == date) {
    177             return null;
    178         }
    179         return date_sdf.format(date);
    180     }
    181     /**
    182      * 日期转换为字符串
    183      * 
    184      * @param date
    185      *            日期
    186      * @param format
    187      *            日期格式
    188      * @return 字符串
    189      */
    190     public static String getDate(String format) {
    191         Date date=new Date();
    192         if (null == date) {
    193             return null;
    194         }
    195         SimpleDateFormat sdf = new SimpleDateFormat(format);
    196         return sdf.format(date);
    197     }
    198 
    199     /**
    200      * 指定毫秒数的时间戳
    201      * 
    202      * @param millis
    203      *            毫秒数
    204      * @return 指定毫秒数的时间戳
    205      */
    206     public static Timestamp getTimestamp(long millis) {
    207         return new Timestamp(millis);
    208     }
    209 
    210     /**
    211      * 以字符形式表示的时间戳
    212      * 
    213      * @param time
    214      *            毫秒数
    215      * @return 以字符形式表示的时间戳
    216      */
    217     public static Timestamp getTimestamp(String time) {
    218         return new Timestamp(Long.parseLong(time));
    219     }
    220 
    221     /**
    222      * 系统当前的时间戳
    223      * 
    224      * @return 系统当前的时间戳
    225      */
    226     public static Timestamp getTimestamp() {
    227         return new Timestamp(new Date().getTime());
    228     }
    229 
    230     /**
    231      * 指定日期的时间戳
    232      * 
    233      * @param date
    234      *            指定日期
    235      * @return 指定日期的时间戳
    236      */
    237     public static Timestamp getTimestamp(Date date) {
    238         return new Timestamp(date.getTime());
    239     }
    240 
    241     /**
    242      * 指定日历的时间戳
    243      * 
    244      * @param cal
    245      *            指定日历
    246      * @return 指定日历的时间戳
    247      */
    248     public static Timestamp getCalendarTimestamp(Calendar cal) {
    249         return new Timestamp(cal.getTime().getTime());
    250     }
    251 
    252     public static Timestamp gettimestamp() {
    253         Date dt = new Date();
    254         DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    255         String nowTime = df.format(dt);
    256         java.sql.Timestamp buydate = java.sql.Timestamp.valueOf(nowTime);
    257         return buydate;
    258     }
    259 
    260     // ////////////////////////////////////////////////////////////////////////////
    261     // getMillis
    262     // 各种方式获取的Millis
    263     // ////////////////////////////////////////////////////////////////////////////
    264 
    265     /**
    266      * 系统时间的毫秒数
    267      * 
    268      * @return 系统时间的毫秒数
    269      */
    270     public static long getMillis() {
    271         return new Date().getTime();
    272     }
    273 
    274     /**
    275      * 指定日历的毫秒数
    276      * 
    277      * @param cal
    278      *            指定日历
    279      * @return 指定日历的毫秒数
    280      */
    281     public static long getMillis(Calendar cal) {
    282         return cal.getTime().getTime();
    283     }
    284 
    285     /**
    286      * 指定日期的毫秒数
    287      * 
    288      * @param date
    289      *            指定日期
    290      * @return 指定日期的毫秒数
    291      */
    292     public static long getMillis(Date date) {
    293         return date.getTime();
    294     }
    295 
    296     /**
    297      * 指定时间戳的毫秒数
    298      * 
    299      * @param ts
    300      *            指定时间戳
    301      * @return 指定时间戳的毫秒数
    302      */
    303     public static long getMillis(Timestamp ts) {
    304         return ts.getTime();
    305     }
    306 
    307     // ////////////////////////////////////////////////////////////////////////////
    308     // formatDate
    309     // 将日期按照一定的格式转化为字符串
    310     // ////////////////////////////////////////////////////////////////////////////
    311 
    312     /**
    313      * 默认方式表示的系统当前日期,具体格式:年-月-日
    314      * 
    315      * @return 默认日期按“年-月-日“格式显示
    316      */
    317     public static String formatDate() {
    318         return date_sdf.format(getCalendar().getTime());
    319     }
    320     /**
    321      * 获取时间字符串
    322      */
    323     public static String getDataString(SimpleDateFormat formatstr) {
    324         return formatstr.format(getCalendar().getTime());
    325     }
    326     /**
    327      * 指定日期的默认显示,具体格式:年-月-日
    328      * 
    329      * @param cal
    330      *            指定的日期
    331      * @return 指定日期按“年-月-日“格式显示
    332      */
    333     public static String formatDate(Calendar cal) {
    334         return date_sdf.format(cal.getTime());
    335     }
    336 
    337     /**
    338      * 指定日期的默认显示,具体格式:年-月-日
    339      * 
    340      * @param date
    341      *            指定的日期
    342      * @return 指定日期按“年-月-日“格式显示
    343      */
    344     public static String formatDate(Date date) {
    345         return date_sdf.format(date);
    346     }
    347 
    348     /**
    349      * 指定毫秒数表示日期的默认显示,具体格式:年-月-日
    350      * 
    351      * @param millis
    352      *            指定的毫秒数
    353      * @return 指定毫秒数表示日期按“年-月-日“格式显示
    354      */
    355     public static String formatDate(long millis) {
    356         return date_sdf.format(new Date(millis));
    357     }
    358 
    359     /**
    360      * 默认日期按指定格式显示
    361      * 
    362      * @param pattern
    363      *            指定的格式
    364      * @return 默认日期按指定格式显示
    365      */
    366     public static String formatDate(String pattern) {
    367         return getSDFormat(pattern).format(getCalendar().getTime());
    368     }
    369 
    370     /**
    371      * 指定日期按指定格式显示
    372      * 
    373      * @param cal
    374      *            指定的日期
    375      * @param pattern
    376      *            指定的格式
    377      * @return 指定日期按指定格式显示
    378      */
    379     public static String formatDate(Calendar cal, String pattern) {
    380         return getSDFormat(pattern).format(cal.getTime());
    381     }
    382 
    383     /**
    384      * 指定日期按指定格式显示
    385      * 
    386      * @param date
    387      *            指定的日期
    388      * @param pattern
    389      *            指定的格式
    390      * @return 指定日期按指定格式显示
    391      */
    392     public static String formatDate(Date date, String pattern) {
    393         return getSDFormat(pattern).format(date);
    394     }
    395 
    396     // ////////////////////////////////////////////////////////////////////////////
    397     // formatTime
    398     // 将日期按照一定的格式转化为字符串
    399     // ////////////////////////////////////////////////////////////////////////////
    400 
    401     /**
    402      * 默认方式表示的系统当前日期,具体格式:年-月-日 时:分
    403      * 
    404      * @return 默认日期按“年-月-日 时:分“格式显示
    405      */
    406     public static String formatTime() {
    407         return time_sdf.format(getCalendar().getTime());
    408     }
    409     /**
    410      * 默认方式表示的系统当前日期,具体格式:年-月-日 时:分:秒
    411      * 
    412      * @return 默认日期按“年-月-日 时:分:秒“格式显示
    413      */
    414     public static String formatTimeyyyyMMddHHmmss(long millis) {
    415         return datetimeFormat.format(new Date(millis));
    416     }
    417     /**
    418      * 指定毫秒数表示日期的默认显示,具体格式:年-月-日 时:分
    419      * 
    420      * @param millis
    421      *            指定的毫秒数
    422      * @return 指定毫秒数表示日期按“年-月-日 时:分“格式显示
    423      */
    424     public static String formatTime(long millis) {
    425         return time_sdf.format(new Date(millis));
    426     }
    427 
    428     /**
    429      * 指定日期的默认显示,具体格式:年-月-日 时:分
    430      * 
    431      * @param cal
    432      *            指定的日期
    433      * @return 指定日期按“年-月-日 时:分“格式显示
    434      */
    435     public static String formatTime(Calendar cal) {
    436         return time_sdf.format(cal.getTime());
    437     }
    438 
    439     /**
    440      * 指定日期的默认显示,具体格式:年-月-日 时:分
    441      * 
    442      * @param date
    443      *            指定的日期
    444      * @return 指定日期按“年-月-日 时:分“格式显示
    445      */
    446     public static String formatTime(Date date) {
    447         return time_sdf.format(date);
    448     }
    449 
    450     // ////////////////////////////////////////////////////////////////////////////
    451     // formatShortTime
    452     // 将日期按照一定的格式转化为字符串
    453     // ////////////////////////////////////////////////////////////////////////////
    454 
    455     /**
    456      * 默认方式表示的系统当前日期,具体格式:时:分
    457      * 
    458      * @return 默认日期按“时:分“格式显示
    459      */
    460     public static String formatShortTime() {
    461         return short_time_sdf.format(getCalendar().getTime());
    462     }
    463 
    464     /**
    465      * 指定毫秒数表示日期的默认显示,具体格式:时:分
    466      * 
    467      * @param millis
    468      *            指定的毫秒数
    469      * @return 指定毫秒数表示日期按“时:分“格式显示
    470      */
    471     public static String formatShortTime(long millis) {
    472         return short_time_sdf.format(new Date(millis));
    473     }
    474 
    475     /**
    476      * 指定日期的默认显示,具体格式:时:分
    477      * 
    478      * @param cal
    479      *            指定的日期
    480      * @return 指定日期按“时:分“格式显示
    481      */
    482     public static String formatShortTime(Calendar cal) {
    483         return short_time_sdf.format(cal.getTime());
    484     }
    485 
    486     /**
    487      * 指定日期的默认显示,具体格式:时:分
    488      * 
    489      * @param date
    490      *            指定的日期
    491      * @return 指定日期按“时:分“格式显示
    492      */
    493     public static String formatShortTime(Date date) {
    494         return short_time_sdf.format(date);
    495     }
    496 
    497     // ////////////////////////////////////////////////////////////////////////////
    498     // parseDate
    499     // parseCalendar
    500     // parseTimestamp
    501     // 将字符串按照一定的格式转化为日期或时间
    502     // ////////////////////////////////////////////////////////////////////////////
    503 
    504     /**
    505      * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
    506      * 
    507      * @param src
    508      *            将要转换的原始字符窜
    509      * @param pattern
    510      *            转换的匹配格式
    511      * @return 如果转换成功则返回转换后的日期
    512      * @throws ParseException
    513      * @throws AIDateFormatException
    514      */
    515     public static Date parseDate(String src, String pattern)
    516             throws ParseException {
    517         return getSDFormat(pattern).parse(src);
    518 
    519     }
    520 
    521     /**
    522      * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
    523      * 
    524      * @param src
    525      *            将要转换的原始字符窜
    526      * @param pattern
    527      *            转换的匹配格式
    528      * @return 如果转换成功则返回转换后的日期
    529      * @throws ParseException
    530      * @throws AIDateFormatException
    531      */
    532     public static Calendar parseCalendar(String src, String pattern)
    533             throws ParseException {
    534 
    535         Date date = parseDate(src, pattern);
    536         Calendar cal = Calendar.getInstance();
    537         cal.setTime(date);
    538         return cal;
    539     }
    540 
    541     public static String formatAddDate(String src, String pattern, int amount)
    542             throws ParseException {
    543         Calendar cal;
    544         cal = parseCalendar(src, pattern);
    545         cal.add(Calendar.DATE, amount);
    546         return formatDate(cal);
    547     }
    548 
    549     /**
    550      * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
    551      * 
    552      * @param src
    553      *            将要转换的原始字符窜
    554      * @param pattern
    555      *            转换的匹配格式
    556      * @return 如果转换成功则返回转换后的时间戳
    557      * @throws ParseException
    558      * @throws AIDateFormatException
    559      */
    560     public static Timestamp parseTimestamp(String src, String pattern)
    561             throws ParseException {
    562         Date date = parseDate(src, pattern);
    563         return new Timestamp(date.getTime());
    564     }
    565 
    566     // ////////////////////////////////////////////////////////////////////////////
    567     // dateDiff
    568     // 计算两个日期之间的差值
    569     // ////////////////////////////////////////////////////////////////////////////
    570 
    571     /**
    572      * 计算两个时间之间的差值,根据标志的不同而不同
    573      * 
    574      * @param flag
    575      *            计算标志,表示按照年/月/日/时/分/秒等计算
    576      * @param calSrc
    577      *            减数
    578      * @param calDes
    579      *            被减数
    580      * @return 两个日期之间的差值
    581      */
    582     public static int dateDiff(char flag, Calendar calSrc, Calendar calDes) {
    583 
    584         long millisDiff = getMillis(calSrc) - getMillis(calDes);
    585 
    586         if (flag == 'y') {
    587             return (calSrc.get(calSrc.YEAR) - calDes.get(calDes.YEAR));
    588         }
    589 
    590         if (flag == 'd') {
    591             return (int) (millisDiff / DAY_IN_MILLIS);
    592         }
    593 
    594         if (flag == 'h') {
    595             return (int) (millisDiff / HOUR_IN_MILLIS);
    596         }
    597 
    598         if (flag == 'm') {
    599             return (int) (millisDiff / MINUTE_IN_MILLIS);
    600         }
    601 
    602         if (flag == 's') {
    603             return (int) (millisDiff / SECOND_IN_MILLIS);
    604         }
    605 
    606         return 0;
    607     }
    608     /**
    609      * String类型 转换为Date,
    610      * 如果参数长度为10 转换格式”yyyy-MM-dd“
    611      *如果参数长度为19 转换格式”yyyy-MM-dd HH:mm:ss“
    612      * * @param text
    613      *             String类型的时间值
    614      */
    615     public void setAsText(String text) throws IllegalArgumentException {
    616         if (StringUtils.hasText(text)) {
    617             try {
    618                 if (text.indexOf(":") == -1 && text.length() == 10) {
    619                     setValue(this.date_sdf.parse(text));
    620                 } else if (text.indexOf(":") > 0 && text.length() == 19) {
    621                     setValue(this.datetimeFormat.parse(text));
    622                 } else {
    623                     throw new IllegalArgumentException(
    624                             "Could not parse date, date format is error ");
    625                 }
    626             } catch (ParseException ex) {
    627                 IllegalArgumentException iae = new IllegalArgumentException(
    628                         "Could not parse date: " + ex.getMessage());
    629                 iae.initCause(ex);
    630                 throw iae;
    631             }
    632         } else {
    633             setValue(null);
    634         }
    635     }
    636     public static int getYear(){
    637         GregorianCalendar calendar=new GregorianCalendar();
    638         calendar.setTime(getDate());
    639         return calendar.get(Calendar.YEAR);
    640       }
    641 
    642     public static void main(String[] args) throws ParseException {
    643         System.out.println(DateUtils.formatTimeyyyyMMddHHmmss(1462666089 * 1000L));
    644         System.out.println(DateUtils.getMillis(DateUtils.parseDate("2016-07-08 08:08:09", "yyyy-mm-dd hh:mm:ss")));
    645     }
    646 }
  • 相关阅读:
    redhat 7.2 内网安装docker
    使用dockerfile 创建ubuntu ssh镜像
    docker 离线环境安装oracle
    redhat 6.6 、7、Centos7离线安装docker
    用命令行管理aws s3
    Anaconda介绍、安装及使用教程
    python2 编码问题万能钥匙
    从mongo数据库中导出数据的方法
    MongoDB学习第三篇 --- Insert操作
    MongoDB学习笔记(一)-Insert操作
  • 原文地址:https://www.cnblogs.com/hpj1038361347/p/7690682.html
Copyright © 2011-2022 走看看