Java中日期时间格式化方法SimpleDateFormat和DateTimeFormatter使用完整示例及区别说明

这篇具有很好参考价值的文章主要介绍了Java中日期时间格式化方法SimpleDateFormat和DateTimeFormatter使用完整示例及区别说明。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

示例代码:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author wh445306
 * @version 1.0
 * @Description DateFormat
 * @Date 2023-04-06 17:10
 */
public class DateFormat {

    public static void main(String[] args) throws ParseException {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        System.out.println(dateString);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        String dateString2 = dateFormat.format(currentTime);
        System.out.println(dateString2);

        Date date1 = dateFormat.parse(dateString2);
        String dateString3 = formatter.format(date1);
        System.out.println(dateString3);


        // 将日期字符串转换为LocalDate对象
        LocalDate date = LocalDate.parse("2023-04-06");
        // 将LocalDate对象格式化为指定格式的字符串
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy年M月d日");
        String formattedDate = date.format(formatter1);
        System.out.println(formattedDate);
        // 将格式化后的字符串转换为LocalDate对象
        LocalDate parsedDate = LocalDate.parse(formattedDate, formatter1);
        // 将LocalDate对象转换为指定格式的字符串
        String parsedDateString = parsedDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println(parsedDateString);
        // 确认两个日期字符串相等
        assert parsedDateString.equals("2023-04-06");
        // 确认两个LocalDate对象相等
        assert parsedDate.equals(date);
    }
}

示例截图:

Java中日期时间格式化方法SimpleDateFormat和DateTimeFormatter使用完整示例及区别说明

 这里完整的用两种方法分别实现了日期和String的来回转换,鉴于SimpleDateFormat早已过时,且非线程安全,所以推荐大家首选使用DateTimeFormatter,用法基本都是差不多的。变化不大。但是DateTimeFormatter需要Java Level 8(8 - Lambdas, type annotations etc.),需留意。文章来源地址https://www.toymoban.com/news/detail-408964.html

到了这里,关于Java中日期时间格式化方法SimpleDateFormat和DateTimeFormatter使用完整示例及区别说明的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Java格式化日期,时间(三种方法,建议收藏)

    在java中String类格式化的方法,是静态format()用于创建格式化的字符串。 format(String format, Object... args) 新字符串使用本地语言环境,制定字符串格式和参数生成格式化的新字符串。 format(Locale locale, String format, Object... args) 使用指定的语言环境,制定字符串格式和参数生成格式化

    2024年02月15日
    浏览(34)
  • Java8日期时间类LocalDateTime格式化

    LocalDateTime日期时间格式化 LocalDateTime localDateTime = LocalDateTime.now() System.out.println(now.format( DateTimeFormatter.ofPattern(\\\"yyyy-MM-dd HH:mm:ss\\\") )); 测试1 测试2 测试2的结果

    2024年02月08日
    浏览(37)
  • StringBuilder类- StringBuffer类- 正则表达式- Date类 (日期)- SimpleDateFormat类(日期格式化的类)- Calendar类 (日历类)

    目录 stringbuilder类 StringBuffer类 正则表达式 日期 日期格式化的类 Calendar类:(日历类) 是一个抽象类 stringbuilder类 概念:可以改变的字符串 (这里跟string可以区分开,string创建的是不可改变的字符串) 底层:使用的是一个byte类型的数组,默认长度16 (string的底层使用final修

    2024年01月17日
    浏览(40)
  • 【js】时间和时间戳转换、日期格式化

    1、时间戳转换日期方法 (格式:2023-08-17) 2、日期字符串转时间戳 3、时间戳转换日期+时间方法 date:时间戳数字(格式:2023-08-17 14:11:01) 4、 获取日期中文格式

    2024年02月12日
    浏览(39)
  • 【JavaScript】JavaScript日期和时间的格式化:

    一、日期和时间的格式化 1、原生方法 【1】使用 toLocaleString 方法 【2】使用 Intl.DateTimeFormat 对象 2、使用字符串操作方法 3、自定义格式化函数 【1】不可指定格式的格式化函数 【2】可指定格式的格式化函数 4、使用第三方库 二、日期和时间的其它常用处理方法 1、创建 Dat

    2024年02月10日
    浏览(32)
  • Windows bat 批处理 日期时间格式化

    有一个批处理脚本,脚本中根据当前日期,动态的生成日志文件, 如:当前是 2023年06月20日,我希望生成的日志文件名为:XX_20230620.log Windows 在批处理中 获取日期和时间的方式如下: echo %time% 输出的时间格式: HH:MM:SS.NN HH :时 MM :分 SS :秒 NN :厘秒(注意不是毫秒,1秒

    2024年02月11日
    浏览(55)
  • sqlite3日期时间格式化和自动输入

    Sqlite3系列:初步💎where💎select sqlite中并未提供单独的日期时间类型,但提供了三种时间表示方式 通过text来存储时间文本 用整型来存储时间戳,时间戳是从1970-01-01算起的秒数 用浮点型来存储自儒略日开始算起的天数,儒略日即公元前4713年1月1日中午12点。 并且提供了一些

    2024年02月06日
    浏览(38)
  • Flutter/Dart日期格式化及时间戳转换

    点击进入我的自建博客链接 Dart 获取当前时间,以及获取当前年、月、日等。 创建指定时间还可以直接从符合日期格式的字符串直接转换,如下。 日期字符串转为时间 日期时间转成时间戳 时间戳转日期时间 可以给某个时间增加或减少时间段(Duration)。

    2024年02月11日
    浏览(38)
  • JavaScript 日期和时间的格式化大汇总(收集)

    一、日期和时间的格式化 1、原生方法 1.1、使用 toLocaleString 方法 Date 对象有一个 toLocaleString 方法,该方法可以根据本地时间和地区设置格式化日期时间。例如:   toLocaleString 方法接受两个参数,第一个参数是地区设置,第二个参数是选项,用于指定日期时间格式和时区信息

    2024年02月08日
    浏览(38)
  • 日期格式化的最佳实践:如何在Java中处理日期格式化

    当涉及到日期格式化时,了解正确的方式和最佳实践是至关重要的。 日期格式化是将日期转换为特定格式的过程,以便在应用程序开发中更好地展示、存储或交互。 以下内容展示常用的三种方式 在Java中,你可以使用java.time.format.DateTimeFormatter类来格式化日期,并将格式化后

    2024年02月07日
    浏览(48)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包