DateTimeFormatter 的操作与使用 -- 通俗易懂
在上一章我们讲解了LocalDate、LocalTime、LocalDateTime、Instant的操作与使用,下面讲解它们之间是如何进行格式化
DateTimeFormatter这个类它只提供了时间格式化的类型,就是按你指定的格式,或者按jdk默认的格式,需要进行调用的则是时间类本身来进行调用才能进行格式化
LocalDate、LocalTime 的api是有2个方法,分别是:parse()、format()方法,时间类型的转换可以调用这2个来进行日期时间类型的转换
E parse(CharSequence text) E parse(CharSequence text, DateTimeFormatter formatter) String format(DateTimeFormatter formatter)
1.字符串转换成日期时间类型
private static void testStringT0LocalDate() {
// String --> LocalDate
LocalDate localDate = LocalDate.parse("2019-12-07");
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
System.out.println(LocalDate.parse("2019-10-09").format(pattern));
// String --> LocalTime
LocalTime localTime = LocalTime.parse("07:43:53"); // String -->LocalDateTime
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
LocalDate localDate = LocalDate.parse("2019-12-07 07:43:53",formatter); System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDate);
}
2.日期时间类型转换成字符串
private static void testLocalDateToString() {
//localDate --> String
LocalDate localDate = LocalDate.now();
String format1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE); //yyyyMMdd
String format2 = localDate.format(DateTimeFormatter.ISO_DATE); //yyyy-MM-dd //2.LocalTime --> String
LocalTime localTime = LocalTime.now();
String format3 = localTime.format(DateTimeFormatter.ISO_TIME); //20:19:22.42
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss");
String format4 = localTime.format(formatter); //3.LocalDateTime --> String
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
String format5 = localDateTime.format(formatter2); System.out.println(format1);
System.out.println(format2);
System.out.println(format3);
System.out.println(format4);
System.out.println(format5); }
DateTimeFormatter 的操作与使用 -- 通俗易懂的更多相关文章
- MongoDB学习系列(2)--使用PHP访问MongoDB
第一部分:介绍 在Windows上安装最新MongoDB步骤非常的简单,这里不做介绍.但是如果你安装的时候没有将MongoDB作为服务运行,每次你都要使用cmd切换到指定的目录下,然后在cmd中启动M ...
- C语言 链表的使用(链表的增删查改,链表逆转,链表排序)
//链表的使用 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include< ...
- 简易四则运算生成程序——添加GUI支持
项目成员:张金生 张政 工程地址: https://coding.net/u/jx8zjs/p/paperOne/git ssh://git@git.coding.net:jx8zjs/pap ...
- 办公达人私藏的EXCEL辅助工具,一人抵十人,高效办公就靠它了!
有很多小伙伴在日常工作中都离不开EXCEL的使用,但EXCEL实在是有太多困难.又复杂的操作,时间紧任务重这一天又废柴了,哎! 别担心,今天将为您分享个逆天强大的EXCEL辅助工具,帮大家快速搞定—— ...
- 设计模式(十五)——命令模式(Spring框架的JdbcTemplate源码分析)
1 智能生活项目需求 看一个具体的需求 1) 我们买了一套智能家电,有照明灯.风扇.冰箱.洗衣机,我们只要在手机上安装 app 就可以控制对这些家电工作. 2) 这些智能家电来自不同的厂家,我们不想针 ...
- JDBC(通俗易懂)简单的操作(增、删、改、查)
项目所写的类: 说明:①.DButil 动态加载数据库驱动,以及获取java.sql.Connection的对象. ②.Personmodel 数据库列(栏位)所对应的字段,定义了相应的set和get ...
- ZooKeeper的API操作(二)(通俗易懂)
所需要6个jar包,都是解压zookeeper的tar包后里面的. zookeeper-3.4.10.jar jline-0.094.jar log4j-1.2.16.jar netty- ...
- geotrellis使用(五)使用scala操作Accumulo
要想搞明白Geotrellis的数据处理情况,首先要弄清楚数据的存放,Geotrellis将数据存放在Accumulo中. Accumulo是一个分布式的Key Value型NOSQL数据库,官网为( ...
- 【转】能否用讲个故事的方式,由浅入深,通俗易懂地解释一下什么是天使投资,VC,PE.
能否用讲个故事的方式,由浅入深,通俗易懂地解释一下什么是天使投资,VC,PE 今天在知乎上看到一篇文章,觉得值得一转的,Here. 我给楼主讲个完整点的故事吧.长文慎点,前方高能,自备避雷针.18岁以 ...
随机推荐
- 备用shell管理方案之butterfly+nginx+https
安装butterfly+nginx https 1. 安装butterfly yum install python36 python36-pip python36-devel -y pip insta ...
- java使用freemark生成word/pdf
目录 一. 背景 二.实现的技术选型以及遇到的坑 三.最终的效果 2.1 .doc word效果展示 2.1 .docx word效果展示 2.2 docx word转pdf效果展示 三.准备工作及代 ...
- ZR#984
ZR#984 解法: 异或的一个性质: $ a+b \geq a \bigoplus b$ 所以一边读入一边把读进来的值加到答案就行了. #include<iostream> #inclu ...
- Python 之pyinstaller模块打包成exe文件
一.安装pyinstaller pip install pyinstaller 二.下载安装pyinstaler运行时所需要的windows扩展pywin32 https://github.com/m ...
- Spring 源码学习之环境搭建
一.下载Spring 源码 进入 https://github.com/spring-projects/spring-framework/tags 选择下载spring freamework的版本 h ...
- dubbo线程模型配置
首先了解一下dubbo线程模型 如果事件处理的逻辑能迅速完成,并且不会发起新的IO请求,比如只是在内存中记个标识.则直接在IO线程上处理更快,因为减少了线程池调度. 但如果事件处理逻辑较慢,或者需要发 ...
- Idea2019.2破解
1.在 hosts 文件里加入如下的配置:C:\WINDOWS\system32\drivers\etc\hosts 0.0.0.0 https://account.jetbrains.com:443 ...
- vue.js的app.js太大怎么优化?
vue.js的app.js太大怎么优化? # http://nginx.org/en/docs/http/ngx_http_gzip_module.htmlgzip on;gzip_min_lengt ...
- 021 CSS高级特性
一:元素的显示与影藏 1.比较常见的单词 dispaly,visibility,overflow 2.display案例 如果影藏了,这个元素就看不见了,然后也不保留位置 <!DOCTYPE h ...
- pip安装各种模块
date: 2019-08-20 19:27:09 pip install requests pip install xpinyin pip install redis