localdatedate
java时间类型详解:localdate与date互转及字符串格式化
在java中,时间处理是一个常见的需求。随着java 8引入了新的日期时间api(java.time包),开发者可以更方便地处理日期和时间。本文将详细介绍 localdate 与 date 类型的互转、字符串格式化以及其他常见时间类型的转换。
一、localdate与date类型互转
1.date转localdate
date 是java早期的日期时间类,而 localdate 是java 8引入的日期类。我们可以通过以下方式将 date 转换为 localdate:
import java.time.localdate;
import java.time.zoneid;
import java.util.date;
public class datetolocaldate {
public static void main(string[] args) {
// 获取当前date对象
date nowdate = new date();
// 将date转换为localdate
localdate localdate = nowdate.toinstant() // 将date转换为instant
.atzone(zoneid.systemdefault()) // 指定时区
.tolocaldate(); // 转换为localdate
system.out.println("date: " + nowdate);
system.out.println("localdate: " + localdate);
}
}输出示例:
date: mon oct 09 12:34:56 cst 2023
localdate: 2023-10-09
2.localdate转date
将 localdate 转换为 date 需要借助 zoneddatetime 或 instant:
import java.time.localdate;
import java.time.zoneid;
import java.util.date;
public class localdatetodate {
public static void main(string[] args) {
// 获取当前localdate对象
localdate localdate = localdate.now();
// 将localdate转换为date
date date = date.from(localdate.atstartofday(zoneid.systemdefault()).toinstant());
system.out.println("localdate: " + localdate);
system.out.println("date: " + date);
}
}输出示例:
localdate: 2023-10-09
date: mon oct 09 00:00:00 cst 2023
二、字符串格式化与解析
1.localdate与字符串互转
(1)localdate转字符串
使用 datetimeformatter 可以将 localdate 格式化为字符串:
import java.time.localdate;
import java.time.format.datetimeformatter;
public class localdatetostring {
public static void main(string[] args) {
localdate localdate = localdate.now();
// 定义格式化器
datetimeformatter formatter = datetimeformatter.ofpattern("yyyy-mm-dd");
// 格式化localdate为字符串
string datestring = localdate.format(formatter);
system.out.println("localdate: " + localdate);
system.out.println("formatted string: " + datestring);
}
}输出示例:
localdate: 2023-10-09
formatted string: 2023-10-09
(2)字符串转localdate
将字符串解析为 localdate:
import java.time.localdate;
import java.time.format.datetimeformatter;
public class stringtolocaldate {
public static void main(string[] args) {
string datestring = "2023-10-09";
// 定义格式化器
datetimeformatter formatter = datetimeformatter.ofpattern("yyyy-mm-dd");
// 解析字符串为localdate
localdate localdate = localdate.parse(datestring, formatter);
system.out.println("string: " + datestring);
system.out.println("localdate: " + localdate);
}
}输出示例:
string: 2023-10-09
localdate: 2023-10-09
2.date与字符串互转
(1)date转字符串
使用 simpledateformat 可以将 date 格式化为字符串:
import java.text.simpledateformat;
import java.util.date;
public class datetostring {
public static void main(string[] args) {
date nowdate = new date();
// 定义格式化器
simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");
// 格式化date为字符串
string datestring = formatter.format(nowdate);
system.out.println("date: " + nowdate);
system.out.println("formatted string: " + datestring);
}
}输出示例:
date: mon oct 09 12:34:56 cst 2023
formatted string: 2023-10-09 12:34:56
(2)字符串转date
将字符串解析为 date:
import java.text.parseexception;
import java.text.simpledateformat;
import java.util.date;
public class stringtodate {
public static void main(string[] args) throws parseexception {
string datestring = "2023-10-09 12:34:56";
// 定义格式化器
simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");
// 解析字符串为date
date date = formatter.parse(datestring);
system.out.println("string: " + datestring);
system.out.println("date: " + date);
}
}输出示例:
string: 2023-10-09 12:34:56
date: mon oct 09 12:34:56 cst 2023
三、其他常见时间类型转换
1.localdatetime与date互转
(1)date转localdatetime
import java.time.localdatetime;
import java.time.zoneid;
import java.util.date;
public class datetolocaldatetime {
public static void main(string[] args) {
date nowdate = new date();
// 将date转换为localdatetime
localdatetime localdatetime = nowdate.toinstant()
.atzone(zoneid.systemdefault())
.tolocaldatetime();
system.out.println("date: " + nowdate);
system.out.println("localdatetime: " + localdatetime);
}
}(2)localdatetime转date
import java.time.localdatetime;
import java.time.zoneid;
import java.util.date;
public class localdatetimetodate {
public static void main(string[] args) {
localdatetime localdatetime = localdatetime.now();
// 将localdatetime转换为date
date date = date.from(localdatetime.atzone(zoneid.systemdefault()).toinstant());
system.out.println("localdatetime: " + localdatetime);
system.out.println("date: " + date);
}
}2.localtime与date互转
(1)date转localtime
import java.time.localtime;
import java.time.zoneid;
import java.util.date;
public class datetolocaltime {
public static void main(string[] args) {
date nowdate = new date();
// 将date转换为localtime
localtime localtime = nowdate.toinstant()
.atzone(zoneid.systemdefault())
.tolocaltime();
system.out.println("date: " + nowdate);
system.out.println("localtime: " + localtime);
}
}(2)localtime转date
由于 localtime 只包含时间信息,转换为 date 时需要结合日期:
import java.time.localdate;
import java.time.localtime;
import java.time.zoneid;
import java.util.date;
public class localtimetodate {
public static void main(string[] args) {
localtime localtime = localtime.now();
// 将localtime转换为date(需要结合日期)
date date = date.from(localdate.now()
.attime(localtime)
.atzone(zoneid.systemdefault())
.toinstant());
system.out.println("localtime: " + localtime);
system.out.println("date: " + date);
}
}到此这篇关于java时间类型转换处理方式的文章就介绍到这了,更多相关java时间类型转换内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论