• simpledateformat怎么改变格式 SimpleDateFormat 的使用及其 注意事项


    本文主要介绍关于的知识点,对【SimpleDateFormat 的使用及其 注意事项】和【simpledateformat怎么改变格式】有兴趣的朋友可以看下由【怪伽先森】投稿的技术文章,希望该技术和经验能帮到你解决你所遇的【android】相关技术问题。

    simpledateformat怎么改变格式

    0. SimpleDateFormat API 简介

    1. /**
    2. * SimpleDateFormat
    3. * 一个与语言环境相关的格式化日期和分析日期的工具类。
    4. * 利用该类可以将日期转换成文本,或者将文本转换成日期。
    5. *
    6. * 在使用SimpleDateFormat时需要指定一个需要的格式(pattern)来格式日期(Date).
    7. * 在此请注意几个字母大小写的差异:
    8. *
    9. * 大写的H为24小时制表示一天中的小时数(0-23)
    10. * 小写的h为12小时制表示一天中的小时数(1-12)
    11. *
    12. * 大写的M表示年中的月份
    13. * 小写的m表示小时中的分钟数
    14. *
    15. * 大写的S表示毫秒数
    16. * 小写的s表示秒数
    17. *
    18. * 所以最常用的24小时制的具体日期的pattern为:
    19. * yyyy-MM-dd HH:mm:ss
    20. *
    21. *
    22. * SimpleDateFormat中format()方法小结:
    23. * 1 format()方法的作用是将日期(Date)转换为文本
    24. * 2 format()方法的输入参数是一个Date
    25. *
    26. *
    27. * SimpleDateFormat中parse()方法小结:
    28. * 1 parse()方法的作用是将文本转换为日期(Date)
    29. * 2 parse()方法的输入参数是一个文本,比如String
    30. */

    1. 将日期转换为文本

    1. import java.text.SimpleDateFormat;
    2. import java.util.Calendar;
    3. import java.util.Date;
    4. import java.util.Locale;
    5. Date date = new Date();
    6. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    7. String time = simpleDateFormat.format(date);
    8. System.out.println("----> 格式化后的日期为: "+time);
    9. System.out.println("----------------------------------");

    输出:

    1. System.out: ----> 格式化后的日期为: 2019-05-23 22:28:01
    2. System.out: ----------------------------------

    2. 将文本转换为日期

    1. /**
    2. * 请注意:
    3. *
    4. * 文本的格式应该与 SimpleDateFormat 中的 pattern 保持一致,否则导致异常
    5. * 比如:
    6. * 2008年08月18日 20:07:33 对应于yyyy年MM月dd日 HH:mm:ss
    7. * 2008-08-18 20:07:33 对应于yyyy-MM-dd HH:mm:ss
    8. */
    9. private void test2() {
    10. try {
    11. String day = "2008年08月18日 20:07:33";
    12. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss", Locale.getDefault());
    13. Date date = simpleDateFormat.parse(day);
    14. System.out.println("----> 格式化后的日期为: "+date);
    15. day = "2008-08-18 20:07:33";
    16. simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    17. date = simpleDateFormat.parse(day);
    18. System.out.println("----> 格式化后的日期为: "+date);
    19. day = "20131227085009";
    20. simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault());
    21. date = simpleDateFormat.parse(day);
    22. simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    23. String time = simpleDateFormat.format(date);
    24. System.out.println("----> 时间文本为: "+time);
    25. System.out.println("----------------------------------");
    26. } catch (Exception e) {
    27. System.out.println("----> Exception: "+e.toString());
    28. }
    29. }

    输出:

    1. System.out: ----> 格式化后的日期为: Mon Aug 18 20:07:33 GMT+08:00 2008
    2. System.out: ----> 格式化后的日期为: Mon Aug 18 20:07:33 GMT+08:00 2008
    3. System.out: ----> 时间文本为: 2013-12-27 08:50:09

    3. 将时间戳转换成时间

    1. long timeStamp=System.currentTimeMillis();
    2. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    3. Date date = new Date(timeStamp);
    4. String time = simpleDateFormat.format(date);
    5. System.out.println("----> 将时间戳转换为字符串: "+time);
    6. System.out.println("----------------------------------");

    输出:

    System.out: ----> 将时间戳转换为字符串: 2019-05-23 22:36:27
    

    4. 将时间转换成时间戳

    1. long timeStamp = System.currentTimeMillis();
    2. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    3. Date date = new Date(timeStamp);
    4. String time = simpleDateFormat.format(date);
    5. System.out.println("----> 当前时间戳为: "+timeStamp+" ,其字符串为:"+time);
    6. Date parsedDate = simpleDateFormat.parse(time);
    7. long ts = parsedDate.getTime();
    8. System.out.println("----> 将字符串转换为时间戳: "+ts);
    9. System.out.println("----------------------------------");

    输出:

    1. ----> 当前时间戳为: 1558622317494 ,其字符串为:2019-05-23 22:38:37
    2. ----> 将字符串转换为时间戳: 1558622317000

    5. java时间戳与unix时间戳的关系

    1. /**
    2. * java中生成的时间戳精确到毫秒,但unix中精确到秒
    3. * 所以两者相差1000倍
    4. */
    5. long javaTimeStamp = System.currentTimeMillis();
    6. long unixTimeStamp = javaTimeStamp/1000;
    7. System.out.println("----> java时间戳: " + javaTimeStamp+", unix时间戳:" + unixTimeStamp);
    8. System.out.println("----------------------------------");

    输出:

    System.out: ----> java时间戳: 1558622474893 ,unix时间戳:1558622474
    

    6. 计算两个时间的差值

    1. private String time1="2016-01-02 00:00:00";
    2. private String time2="2013-09-21 00:00:00";
    3. private void getTimeDifference(String time1,String time2) {
    4. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    5. try {
    6. Date date1 = simpleDateFormat.parse(time1);
    7. Date date2 = simpleDateFormat.parse(time2);
    8. long difference = date1.getTime() - date2.getTime();
    9. long days = difference / (1000 * 60 * 60 * 24);
    10. System.out.println("----> 两个时间相距:"+days+"天");
    11. } catch (Exception e) {
    12. System.out.println("----> Exception="+e.toString());
    13. }
    14. System.out.println("----------------------------------");
    15. }

    输出:

    System.out: ----> 两个时间相距:833

    7. 比较两个时间的大小

    1. private void compareTime(String time1, String time2) {
    2. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    3. Calendar calendar1 = java.util.Calendar.getInstance();
    4. Calendar calendar2 = java.util.Calendar.getInstance();
    5. try {
    6. calendar1.setTime(dateFormat.parse(time1));
    7. calendar2.setTime(dateFormat.parse(time2));
    8. } catch (java.text.ParseException e) {
    9. System.out.println("----> Exception=" + e.toString());
    10. }
    11. int result = calendar1.compareTo(calendar2);
    12. if (result == 0){
    13. System.out.println("----> time1等于time2");
    14. }else if (result < 0) {
    15. System.out.println("----> time1小于time2");
    16. }else {
    17. System.out.println("----> time1大于time2");
    18. }
    19. }

    输出:

    System.out: ----> time1大于time2
    

    2. 线程安全的使用方法 2.1 ThreadLocal

    1. import java.text.DateFormat;
    2. import java.text.ParseException;
    3. import java.text.SimpleDateFormat;
    4. import java.util.Date;
    5. public class ConcurrentDateUtil {
    6. private static ThreadLocal
    7. threadLocal = new ThreadLocal
    8. () { @Override protected DateFormat initialValue() { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } }; public static Date parse(String dateStr) throws ParseException { return threadLocal.get().parse(dateStr); } public static String format(Date date) { return threadLocal.get().format(date); } }

    使用 ThreadLocal, 也是将共享变量变为独享,线程独享肯定能比方法独享在并发环境中能减少不少创建对象的开销。如果对性能要求比较高的情况下,一般推荐使用这种方法。

    2.2 Java 8 中的解决办法

    Java 8 提供了新的日期时间 API,其中包括用于日期时间格式化的 DateTimeFormatter,它与 SimpleDateFormat 最大的区别在于:DateTimeFormatter 是线程安全的,而 SimpleDateFormat 并不是线程安全。

    解析日期

    1. String dateStr= "2018年06月20日";
    2. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
    3. LocalDate date= LocalDate.parse(dateStr, formatter);

    日期转换为字符串

    1. LocalDateTime now = LocalDateTime.now();
    2. DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh:mm a");
    3. String nowStr = now.format(format);

    参考链接 Android使用SimpleDateFormat不同手机有时格式化手机不准确
    多线程SimpleDateFormat日期格式化与ThreadLocal用法
    SimpleDateFormat 如何安全的使用?

    本文《SimpleDateFormat 的使用及其 注意事项》版权归怪伽先森所有,引用SimpleDateFormat 的使用及其 注意事项需遵循CC 4.0 BY-SA版权协议。

  • 相关阅读:
    MySQL优化(count、分页、join)及数据类型的选择(思维导图)
    性能 10 年提升 1000 倍,Zilliz 重磅发布 Milvus 2.4,开启 GPU 加速向量数据库新纪元!
    简单了解 TiDB 架构
    Java项目:SSM汽车维修管理系统源码
    芯片后端的APR是指什么?
    Spring IOC的实现机制
    Redis集群高可用环境之主从复制
    120年奥运历史数据分析
    前端埋点实现方案
    mac安装redis及springboot整合redis,简单快速
  • 原文地址:https://blog.csdn.net/m0_62089210/article/details/126399880