Java8日期时间类LocalDateTime格式化

这篇具有很好参考价值的文章主要介绍了Java8日期时间类LocalDateTime格式化。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Java8日期时间类LocalDateTime格式化

LocalDateTime日期时间格式化

LocalDateTime localDateTime = LocalDateTime.now()
System.out.println(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

localDateTime.format(`DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")

测试1

	public static void main(String...arguments)throws Exception{
		
		LocalDateTime now = LocalDateTime.now();		
		
		System.out.println(now);
		System.out.println(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
		System.out.println(now.format(DateTimeFormatter.BASIC_ISO_DATE));
		System.out.println(now.format(DateTimeFormatter.ISO_DATE));
		System.out.println(now.format(DateTimeFormatter.ISO_DATE_TIME));
		System.out.println(now.format(DateTimeFormatter.ISO_TIME));
//		System.out.println(now.format(DateTimeFormatter.ISO_INSTANT));  //会报错
		System.out.println(now.format(DateTimeFormatter.ISO_LOCAL_DATE));
		System.out.println(now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
		System.out.println(now.format(DateTimeFormatter.ISO_LOCAL_TIME));
//		System.out.println(now.format(DateTimeFormatter.ISO_OFFSET_DATE));   //会报错
//		System.out.println(now.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));     //会报错
//		System.out.println(now.format(DateTimeFormatter.ISO_OFFSET_TIME));    //会报错
		System.out.println(now.format(DateTimeFormatter.ISO_ORDINAL_DATE));
		System.out.println(now.format(DateTimeFormatter.ISO_WEEK_DATE));
//		System.out.println(now.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));    //会报错
//		System.out.println(now.format(DateTimeFormatter.RFC_1123_DATE_TIME));   //会报错
	}

测试2

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class TestLocalDateTime230612 {
	
	public static void main(String...arguments)throws Exception{
		
		LocalDateTime now = LocalDateTime.now();
		
		
		pln("原样toString()" , now.toString());
		pln("DateTimeFormatter.ofPattern(\"yyyy-MM-dd HH:mm:ss\")" , now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
		pln("DateTimeFormatter.BASIC_ISO_DATE)" , now.format(DateTimeFormatter.BASIC_ISO_DATE));
		pln("DateTimeFormatter.ISO_DATE" , now.format(DateTimeFormatter.ISO_DATE));
		pln("DateTimeFormatter.ISO_DATE_TIME)" , now.format(DateTimeFormatter.ISO_DATE_TIME));
		pln("DateTimeFormatter.ISO_TIME)" , now.format(DateTimeFormatter.ISO_TIME));
//		pln("" , now.format(DateTimeFormatter.ISO_INSTANT));  //会报错
		pln("DateTimeFormatter.ISO_LOCAL_DATE)" , now.format(DateTimeFormatter.ISO_LOCAL_DATE));
		pln("DateTimeFormatter.ISO_LOCAL_DATE_TIME)" , now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
		pln("DateTimeFormatter.ISO_LOCAL_TIME)" , now.format(DateTimeFormatter.ISO_LOCAL_TIME));
//		pln("" , now.format(DateTimeFormatter.ISO_OFFSET_DATE));   //会报错
//		pln("" , now.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));     //会报错
//		pln("" , now.format(DateTimeFormatter.ISO_OFFSET_TIME));    //会报错
		pln("DateTimeFormatter.ISO_ORDINAL_DATE)" , now.format(DateTimeFormatter.ISO_ORDINAL_DATE));
		pln("DateTimeFormatter.ISO_WEEK_DATE)" , now.format(DateTimeFormatter.ISO_WEEK_DATE));
//		pln("" , now.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));    //会报错
//		pln("" , now.format(DateTimeFormatter.RFC_1123_DATE_TIME));   //会报错
	}
	
	static void pln(String a, Object b) {
		while(a.length()<"DateTimeFormatter.ofPattern(\"yyyy-MM-dd HH:mm:ss\")".length())a+=" ";
		System.out.println  (a + "  的效果:  " + b);
	}	
}

测试2的结果文章来源地址https://www.toymoban.com/news/detail-480612.html

原样toString()                                       的效果:  2023-06-12T22:50:54.825257400
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")  的效果:  2023-06-12 22:50:54
DateTimeFormatter.BASIC_ISO_DATE)                   的效果:  20230612
DateTimeFormatter.ISO_DATE                          的效果:  2023-06-12
DateTimeFormatter.ISO_DATE_TIME)                    的效果:  2023-06-12T22:50:54.8252574
DateTimeFormatter.ISO_TIME)                         的效果:  22:50:54.8252574
DateTimeFormatter.ISO_LOCAL_DATE)                   的效果:  2023-06-12
DateTimeFormatter.ISO_LOCAL_DATE_TIME)              的效果:  2023-06-12T22:50:54.8252574
DateTimeFormatter.ISO_LOCAL_TIME)                   的效果:  22:50:54.8252574
DateTimeFormatter.ISO_ORDINAL_DATE)                 的效果:  2023-163
DateTimeFormatter.ISO_WEEK_DATE)                    的效果:  2023-W24-1

到了这里,关于Java8日期时间类LocalDateTime格式化的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

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

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

相关文章

  • 【Java 基础篇】Java日期和时间格式化与解析指南:SimpleDateFormat详解

    日期和时间在软件开发中经常被用到,无论是用于记录事件、计算时间间隔还是格式化日期以供用户友好的展示。Java 提供了强大的日期和时间处理工具,其中 SimpleDateFormat 类是一个重要的工具,用于格式化日期和时间,同时也支持解析日期和时间。本篇博客将深入探讨 Sim

    2024年02月09日
    浏览(46)
  • Java中日期时间格式化方法SimpleDateFormat和DateTimeFormatter使用完整示例及区别说明

    示例代码: 示例截图:  这里完整的用两种方法分别实现了日期和String的来回转换,鉴于SimpleDateFormat早已过时,且非线程安全,所以推荐大家首选使用DateTimeFormatter,用法基本都是差不多的。变化不大。但是DateTimeFormatter需要Java Level 8(8 - Lambdas, type annotations etc.),需留意。

    2023年04月09日
    浏览(28)
  • 【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日
    浏览(31)
  • 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)
  • Windows bat 批处理 日期时间格式化

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

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

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

    2024年02月07日
    浏览(48)
  • 使用 uni-app 开发项目,日期和时间如何格式化?

    功能需求描述 在开发项目时,往往需要对从后端查询到的时间进行格式化,查到的时间格式一般都是 时间戳 ,一堆数字,这时候怎么转化成类似于  2023年8月15日 08:12:10  这样的格式? 在组件显示格式化后的日期 其实 uni-app 的官方拓展组件  uni-dateformat  就能实现这个需求

    2024年02月05日
    浏览(36)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包