欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java开发笔记(四十四)本地日期时间与字符串的互相转换

程序员文章站 2022-05-14 10:09:25
之前介绍Calendar的时候,提到日历实例无法直接输出格式化后的时间字符串,必须先把Calendar类型转换成Date类型,再通过格式化工具SimpleDateFormat获得字符串。而日期时间的格式化恰恰是最常用的场合,这就很尴尬了,原本设计Calendar是想取代Date,结果大家还在继续使用 ......

之前介绍calendar的时候,提到日历实例无法直接输出格式化后的时间字符串,必须先把calendar类型转换成date类型,再通过格式化工具simpledateformat获得字符串。而日期时间的格式化恰恰是最常用的场合,这就很尴尬了,原本设计calendar是想取代date,结果大家还在继续使用date类型,没有达到预期的效果。那么java8重新设计的本地日期时间家族,为了彻底革了date的命,同时推出了自己的格式化器具datetimeformatter,并定义了几种常见的日期时间格式。例如datetimeformatter.basic_iso_date表示的日期格式为yyyymmdd,datetimeformatter.iso_local_date表示的日期格式为yyyy-mm-dd,datetimeformatter.iso_local_time表示的时间格式为hh:mm:ss,datetimeformatter.iso_local_date_time表示的日期时间格式为yyyy-mm-ddthh:mm:ss等等。
现在只要调用本地日期时间的parse方法,即可将字符串形式转换为日期时间,无需像calendar那样还得借助于date。下面是本地日期时间家族结合

		string strdatesimple = "20180729";
		// 把日期字符串转换为localdate实例。basic_iso_date定义的日期格式为yyyymmdd
		localdate datesimple = localdate.parse(strdatesimple, datetimeformatter.basic_iso_date);
		system.out.println("datesimple="+datesimple.tostring());
		string strdatewithline = "2018-07-29";
		// 把日期字符串转换为localdate实例。iso_local_date定义的日期格式为yyyy-mm-dd
		localdate datewithline = localdate.parse(strdatewithline, datetimeformatter.iso_local_date);
		system.out.println("datewithline="+datewithline.tostring());
		string strtimewithcolon = "12:44:50";
		// 把时间字符串转换为localtime实例。iso_local_time定义的时间格式为hh:mm:ss
		localtime timewithcolon = localtime.parse(strtimewithcolon, datetimeformatter.iso_local_time);
		system.out.println("timewithcolon="+timewithcolon.tostring());
		string strdatetimeiso = "2018-11-23t14:46:30";
		// 把日期时间字符串转换为localdatetime实例。iso_local_date_time定义的日期时间格式为yyyy-mm-ddthh:mm:ss
		localdatetime datetimeiso = localdatetime.parse(strdatetimeiso, datetimeformatter.iso_local_date_time);
		system.out.println("datetimeiso="+datetimeiso.tostring());

 

除了系统自带的几种日期时间格式,程序员也可以自己定义其它格式,此时需要调用datetimeformatter的ofpattern方法完成格式定义,使用ofpattern方法得到某个格式化实例,就能直接代入本地日期时间的parse方法之中。本地日期时间详细的自定义格式化代码如下所示:

		string strdatewithsway = "2018/07/29";
		// 自己定义了一个形如“yyyy/mm/dd”的日期格式
		datetimeformatter dateformatwithsway = datetimeformatter.ofpattern("yyyy/mm/dd");
		// 把日期字符串按照格式“yyyy/mm/dd”转换为localdate实例
		localdate datewithsway = localdate.parse(strdatewithsway, dateformatwithsway);
		system.out.println("datewithsway="+datewithsway.tostring());
		string strtimesimple = "125809";
		// 自己定义了一个形如“hhmmss”的时间格式
		datetimeformatter timeformatsimple = datetimeformatter.ofpattern("hhmmss");
		// 把时间字符串按照格式“hhmmss”转换为localtime实例
		localtime timesimple = localtime.parse(strtimesimple, timeformatsimple);
		system.out.println("timesimple="+timesimple.tostring());
		string strwithcn = "2018年07月29日12时58分09秒";
		// 自己定义了一个形如“yyyy年mm月dd日hh时mm分ss秒”的日期时间格式
		datetimeformatter formatwithcn = datetimeformatter.ofpattern("yyyy年mm月dd日hh时mm分ss秒");
		// 把日期时间字符串按照格式“yyyy年mm月dd日hh时mm分ss秒”转换为localdatetime实例
		localdatetime datetimewithcn = localdatetime.parse(strwithcn, formatwithcn);
		system.out.println("datetimewithcn="+datetimewithcn.tostring());

 

既然字符串能够转换为本地日期时间,反过来也可以将本地日期时间转换为字符串,这时parse方法就变成了format方法,具体的转换代码示例如下:

	// 把日期时间转换为字符串
	private static void convertlocaltostring() {
		// 获得当前日期的实例
		localdate date = localdate.now();
		// 把localdate实例转换为日期字符串。basic_iso_date定义的日期格式为yyyymmdd
		string strdatesimple = date.format(datetimeformatter.basic_iso_date);
		system.out.println("strdatesimple="+strdatesimple);
		// 把localdate实例转换为日期字符串。iso_local_date定义的日期格式为yyyy-mm-dd
		string strdatewithline = date.format(datetimeformatter.iso_local_date);
		system.out.println("strdatewithline="+strdatewithline);
		// 自己定义了一个形如“yyyy/mm/dd”的日期格式
		datetimeformatter dateformatwithsway = datetimeformatter.ofpattern("yyyy/mm/dd");
		// 把localdate实例按照格式“yyyy/mm/dd”转换为日期字符串
		string strdatewithsway = date.format(dateformatwithsway);
		system.out.println("strdatewithsway="+strdatewithsway);
		// 获得当前时间的实例
		localtime time = localtime.now();
		// 把localtime实例转换为时间字符串。iso_local_time定义的时间格式为hh:mm:ss
		string strtimewithcolon = time.format(datetimeformatter.iso_local_time);
		system.out.println("strtimewithcolon="+strtimewithcolon);
		// 自己定义了一个形如“hhmmss”的时间格式
		datetimeformatter timeformatsimple = datetimeformatter.ofpattern("hhmmss");
		// 把localtime实例按照格式“hhmmss”转换为时间字符串
		string strtimesimple = time.format(timeformatsimple);
		system.out.println("strtimesimple="+strtimesimple);
		// 获得当前日期时间的实例
		localdatetime datetime = localdatetime.now();
		// 自己定义了一个形如“yyyy年mm月dd日hh时mm分ss秒”的日期时间格式
		datetimeformatter formatwithcn = datetimeformatter.ofpattern("yyyy年mm月dd日hh时mm分ss秒");
		// 把localdatetime实例按照格式“yyyy年mm月dd日hh时mm分ss秒”转换为日期时间字符串
		string strwithcn = datetime.format(formatwithcn);
		system.out.println("strwithcn="+strwithcn);
	}

  

更多java技术文章参见《java开发笔记(序)章节目录