博客
关于我
日期和字符串的相互转换
阅读量:354 次
发布时间:2019-03-04

本文共 1251 字,大约阅读时间需要 4 分钟。

??????????

???????

1. ??????

????

  • ?????dateToString
  • ???
    • date?????
    • type????????????yyyy-MM-dd???yyyy-MM-dd HH:mm:ss?
  • ??????????????

????

public static String dateToString(Object date, String type) {    if (date != null) {        try {            SimpleDateFormat sdf = new SimpleDateFormat(type);            return sdf.format(date);        } catch (Exception e) {            e.printStackTrace();        }    }    return "";}

????

  • ?????date?String?????????Date????????IllegalArgumentException???
  • ??SimpleDateFormat???????????????????

2. ??????

????

  • ?????StringToDate
  • ???
    • dateStr??????
    • type????????????yyyy-MM-dd???yyyy-MM-dd HH:mm:ss?
  • ????Date??

????

public static Date StringToDate(String date, String type) {    try {        SimpleDateFormat sdf = new SimpleDateFormat(type);        return sdf.parse(date);    } catch (Exception e) {        e.printStackTrace();    }    return null;}

3. ?????

????

  • ??StringToDate?dateToString???????????????

????

public static void main(String[] args) {    String type = "yyyy-MM-dd HH:mm:ss"; // ????    long date = 1559014035000L; // ???    Date riqi = StringToDate(date, type);    System.out.println(riqi);    String shiJian = dateToString(riqi, type);    System.out.println(shiJian);}

????

  • ????????????????????????

转载地址:http://lskr.baihongyu.com/

你可能感兴趣的文章
Objective-C实现ABC人工蜂群算法(附完整源码)
查看>>
Objective-C实现activity selection活动选择问题算法(附完整源码)
查看>>
Objective-C实现AC算法(Aho-Corasick) 算法(附完整源码)
查看>>
Objective-C实现adaboost算法(附完整源码)
查看>>
Objective-C实现Adler32算法(附完整源码)
查看>>
Objective-C实现AES算法(附完整源码)
查看>>
Objective-C实现AffineCipher仿射密码算法(附完整源码)
查看>>
Objective-C实现aliquot sum等分求和算法(附完整源码)
查看>>
Objective-C实现all combinations所有组合算法(附完整源码)
查看>>
Objective-C实现all permutations所有排列算法(附完整源码)
查看>>
Objective-C实现all subsequences所有子序列算法(附完整源码)
查看>>
Objective-C实现AlphaNumericalSort字母数字排序算法(附完整源码)
查看>>
Objective-C实现alternate disjoint set不相交集算法(附完整源码)
查看>>
Objective-C实现alternative list arrange备选列表排列算法(附完整源码)
查看>>
Objective-C实现An Armstrong number阿姆斯特朗数算法(附完整源码)
查看>>
Objective-C实现anagrams字谜算法(附完整源码)
查看>>
Objective-C实现ApproximationMonteCarlo蒙特卡洛方法计算pi值算法 (附完整源码)
查看>>
Objective-C实现area under curve曲线下面积算法(附完整源码)
查看>>
Objective-C实现argmax函数功能(附完整源码)
查看>>
Objective-C实现arithmetic算术算法(附完整源码)
查看>>