【基础】SimpleDateFormat日期格式转换详解

SimpleDateFormat是处理日期格式转换的类。

官方API_1.8关于SimpleDateFormat继承于DateFormate截图:

SimpleDateFormat的构造器如下:

SimpleDateFormat中的格式定义,常用的用红色框圈出:

中文解释:

y : 年

M : 年中的月份

D : 年中的天数

d : 月中的天数

w : 年中的周数

W : 月中的周数

a : 上下/下午

H : 一天中的小时数(0-23)

h : 一天中的小时数(0-12)

m : 小时中的分钟

s : 分钟中的秒数

S : 毫秒数

SimpleDateFormat方法:

继承于DateFormate的方法:

 

SimpleDateFormat常用方法和常用格式定义使用实例:

 

package com.lanhuigu.java.format;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FormatTest {
	public static void main(String[] args) throws ParseException {
		// **************1.(format,parse)最常用方法实例*************
		System.out.println("----------最常用方法---------");
		// 格式
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// 时间
		Date date1 = new Date();
		System.out.println("操作前的时间:" + date1);
		// 日期类型时间-》转换为定义格式-》字符串类型时间
		/*
		 * 注意: format(Date date)这个方法来自于SimpleDateFormat的父类DateFormat
		 */
		String str1 = sdf1.format(date1);
		System.out.println("字符串类型时间:" + str1);
		
		// 字符串类型时间-》转换为定义格式-》日期类型时间
		Date dateF1 = sdf1.parse(str1);
		System.out.println("日期类型时间:" + dateF1);
		
		// **************2.关于常用格式分析*************
		System.out.println("----------常用格式分析---------");
		/*
		 * y : 年
		 * M : 年中的月份
		 * D : 年中的天数
		 * d : 月中的天数
		 * w : 年中的周数
		 * W : 月中的周数
		 * a : 上下/下午
		 * H : 一天中的小时数(0-23)
		 * h : 一天中的小时数(0-12)
		 * m : 小时中的分钟
		 * s : 分钟钟的秒数
		 * S : 毫秒数
		 */
		// 注意,为了省事,这个地方把常用的都放进来了,一起打印看效果,
		// 在实际使用中,根据需求进行相应格式转换
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd,w,W,a,HH:mm:ss,SS");
		String str2 = sdf2.format(new Date());
		System.out.println("日期类型时间:" + str2);
		
		System.out.println("字符串类型时间:" + sdf2.parse(str2));
		
		// **************2.关于构造器使用技巧分析*************
		System.out.println("----------构造器使用技巧分析---------");
		/*
		 * 构造器:
		 * SimpleDateFormat();
		 * SimpleDateFormat(String pattern);
		 * SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols);
		 * SimpleDateFormat(String pattern, Locale locale)
		 */
		// 通过对应构造器构造对象,直接调用方法,简洁写法
		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		
	}
}

程序运行结果:

 

 

总结: 关于SimpleDateFormate需会使用其不同参数下的常用方法,以及常用格式,构造器简写方式。

### 回答1: SimpleDateFormat 和 DateFormatter 都是 Java 中用于日期格式化的类,但是 SimpleDateFormat 是旧版的 API,而 DateFormatter 是 Java 8 中引入的新 API。 如果要将 SimpleDateFormat 转换为 DateFormatter,可以通过以下步骤来实现: 1. 首先创建一个 SimpleDateFormat 对象,将日期格式字符串作为参数传入。 2. 然后使用 SimpleDateFormat 对象的 toPattern() 方法获取格式化字符串。 3. 接着创建一个 DateFormatter 对象,并将获取到的格式化字符串作为参数传入。 4. 最后就可以使用 DateFormatter 对象的 format() 方法将日期格式化为指定格式。 示例代码如下: ``` // 创建 SimpleDateFormat 对象 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 获取格式化字符串 String pattern = sdf.toPattern(); // 创建 DateFormatter 对象 DateFormatter df = new DateFormatter(pattern); // 格式化日期 String formattedDate = df.format(new Date()); // 输出格式化后的日期 System.out.println(formattedDate); ``` 上述代码中,我们先创建了一个 SimpleDateFormat 对象,将日期格式字符串设置为 "yyyy-MM-dd",然后使用 toPattern() 方法获取到该格式化字符串。接着我们使用获取到的格式化字符串创建了一个 DateFormatter 对象,最后使用该对象的 format() 方法将当前日期格式化为指定格式,然后输出格式化后的日期。 ### 回答2: SimpleDateFormat是java.text包中的一个类,用于将日期和时间格式化为字符串,也可以将字符串解析为日期和时间。 Java 8之后,SimpleDateFormat被废弃,并推荐使用新的日期和时间API中的DateTimeFormatter类。所以,要将SimpleDateFormat转换为DateTimeFormatter,可以按照以下步骤进行: 1. 首先,导入java.time.format包中的DateTimeFormatter类: import java.time.format.DateTimeFormatter; 2. 创建一个DateTimeFormatter对象,可以使用ofPattern()方法来指定日期和时间的格式。例如,要使用和SimpleDateFormat中相同的格式"yyyy-MM-dd",可以这样创建DateTimeFormatter: DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); 3. 使用该DateTimeFormatter对象来格式化日期和时间。可以使用format()方法来将日期和时间对象转换为字符串。例如,将当前日期格式化为字符串的方法是: String formattedDate = formatter.format(LocalDate.now()); 4. 使用该DateTimeFormatter对象来解析字符串为日期和时间。可以使用parse()方法来将字符串转换日期和时间对象。例如,将字符串"2022-01-01"解析为日期对象的方法是: LocalDate date = LocalDate.parse("2022-01-01", formatter); 通过以上步骤,我们可以使用DateTimeFormatter来替代SimpleDateFormat,并实现与SimpleDateFormat相同的日期和时间格式化和解析功能。同时,使用DateTimeFormatter还可以获得更好的线程安全性和不可变性。 需要注意的是,SimpleDateFormat是线程不安全的,并且Java 8之前的日期和时间API也存在一些问题,所以推荐尽可能使用Java 8及以后的新日期和时间API。 ### 回答3: SimpleDateFormat 是 Java 中的一个类,用于将日期时间格式化成字符串或将字符串解析成日期时间对象。而 Dateformatter 是指定日期时间格式的对象。简而言之,SimpleDateFormat 是一个用于格式化和解析日期时间的类,而 Dateformatter 是通过 Simpledateformat 创建的具体日期时间格式对象。 要将 SimpleDateFormat 转换成 Dateformatter,我们可以使用 SimpleDateFormat 的 toPattern() 方法获取原始的日期时间格式字符串,然后将其传递给 Dateformatter 的 ofPattern() 静态方法,以创建 Dateformatter 对象。 下面是一个示例代码: ```java import java.text.SimpleDateFormat; import java.time.format.DateTimeFormatter; public class DateFormatExample { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String pattern = sdf.toPattern(); DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern); System.out.println("Original format pattern: " + pattern); System.out.println("Dateformatter format pattern: " + dtf.toString()); } } ``` 在上述代码中,我们首先创建了一个 SimpleDateFormat 对象 sdf,并指定日期时间的格式为 "yyyy-MM-dd HH:mm:ss"。然后,通过 sdf 的 toPattern() 方法获取原始的格式字符串,并将其赋值给 pattern 变量。接下来,使用 Dateformatter 的 ofPattern() 静态方法,将 pattern 作为参数创建了一个新的 Dateformatter 对象 dtf。最后,我们打印出了原始格式字符串和 Dateformatter 格式字符串。 这样,我们就成功地将 SimpleDateFormat 转换成了 Dateformatter。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值