1.当前时间对象转字符串
方法一:date和simpledateformat实现
使用java.util.date类和java.text.simpledateformat类
public static void main(string[] args) {
date now = new date();
simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
string currenttime = sdf.format(now);
system.out.println(currenttime);
}
方法二:localdatetime和datetimeformatter
使用java.time.localdatetime类和java.time.format.datetimeformatter类(java 8及以上版本)
public static void main(string[] args) {
localdatetime now = localdatetime.now();
datetimeformatter dtf = datetimeformatter.ofpattern("yyyy-mm-dd hh:mm:ss");
string currenttime = now.format(dtf);
system.out.println(currenttime);
}
方式三:calendar
public static void main(string[] args) {
calendar cal = calendar.getinstance();
int year = cal.get(calendar.year);
int month = cal.get(calendar.month) + 1;
int day = cal.get(calendar.day_of_month);
int hour = cal.get(calendar.hour_of_day);
int minute = cal.get(calendar.minute);
int second = cal.get(calendar.second);
string currenttime = string.format("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second);
system.out.println(currenttime);
}
方式四:localdate和localtime
使用java.time.localdate类和java.time.localtime类(java 8及以上版本)
public static void main(string[] args) {
localdate currentdate = localdate.now();
localtime currenttime = localtime.now();
datetimeformatter dtf = datetimeformatter.ofpattern("yyyy-mm-dd hh:mm:ss");
string formatteddate = currentdate.format(dtf);
string formattedtime = currenttime.format(dtf);
system.out.println(formatteddate + " " + formattedtime);
}
2.时间字符串转时间对象
方式一: simpledateformat
string datestring = "2024-10-28 22:56:11";
simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
sdf.parse(datestring);
方式二:datetimeformatter
datetimeformatter有三种解析模式:
strict:严格模式,日期、时间必须完全正确。
smart:智能模式,针对日可以自动调整。月的范围在 1 到 12,日的范围在 1 到 31。比如输入是 2 月 30 号,当年 2 月只有 28 天,返回的日期就是 2 月 28 日。默认模式
lenient:宽松模式,主要针对月和日,会自动后延。结果类似于localdata#plusdays或者localdate#plusmonths。
smart智能模式:
testtime("2023-02-33 22:56:11");
testtime("2023-02-30 22:56:11");
private static void testtime(string datestring){
datetimeformatter sdf = datetimeformatter.ofpattern("yyyy-mm-dd hh:mm:ss");
try {
localdatetime localdatetime = localdatetime.parse(datestring, sdf);
system.out.println("转换后时间:" + localdatetime);
} catch (exception ex) {
system.err.println("转换失败:" + ex.getmessage());
}
}
lenient宽松模式:
testtime("2023-02-33 22:56:11");
testtime("2023-02-30 22:56:11");
private static void testtime(string datestring){
datetimeformatter sdf = datetimeformatter.ofpattern("yyyy-mm-dd hh:mm:ss")
.withresolverstyle(resolverstyle.lenient);
try {
localdatetime localdatetime = localdatetime.parse(datestring, sdf);
system.out.println("转换后时间:" + localdatetime);
} catch (exception ex) {
system.err.println("转换失败:" + ex.getmessage());
}
}
strict严格模式:
严格模式下时间字符串的年份格式化分为yyyy和uuuu两种。当datetimeformatter.ofpattern()进行非严格模式下的格式化的时候,yyyy/mm/dd和uuuu/mm/dd表现相同,都是转换为合法的日期。
yyyy:
yyyy代表公元纪年,在严格模式下,如果时间字符串不包含公元,因此程序报错。
testtime("2023-02-20 22:56:11");
testtime("2023-02-30 22:56:11");
private static void testtime(string datestring){
datetimeformatter sdf = datetimeformatter.ofpattern("yyyy-mm-dd hh:mm:ss")
.withresolverstyle(resolverstyle.strict);
try {
localdatetime localdatetime = localdatetime.parse(datestring, sdf);
system.out.println("转换后时间:" + localdatetime);
} catch (exception ex) {
system.err.println("转换失败:" + ex.getmessage());
}
}
使用g yyyy/mm/dd来进行格式化 ,并且在时间字符串中也增加了公元单位。
testtime("公元 2023-02-20 22:56:11");
testtime("公元 2023-02-30 22:56:11");
private static void testtime(string datestring){
/**
* locale.japan --> "西暦 yyyy-mm-dd"
* locale.china --> "公元 yyyy-mm-dd"
* locale.us --> "ad yyyy-mm-dd"
*/
datetimeformatter sdf = datetimeformatter.ofpattern("g yyyy-mm-dd hh:mm:ss",locale.chinese)
.withresolverstyle(resolverstyle.strict);
try {
localdatetime localdatetime = localdatetime.parse(datestring, sdf);
system.out.println("转换后时间:" + localdatetime);
} catch (exception ex) {
system.err.println("转换失败:" + ex.getmessage());
}
}
uuuu:
uuuu不代表公元格式化,所以不会存在异常。
testtime("2023-02-20 22:56:11");
testtime("2023-02-29 22:56:11");
private static void testtime(string datestring){
datetimeformatter sdf = datetimeformatter.ofpattern("uuuu-mm-dd hh:mm:ss")
.withresolverstyle(resolverstyle.strict);
try {
localdatetime localdatetime = localdatetime.parse(datestring, sdf);
system.out.println("转换后时间:" + localdatetime);
} catch (exception ex) {
system.err.println("转换失败:" + ex);
}
}
到此这篇关于java实现时间和字符串互转的文章就介绍到这了,更多相关java时间和字符串互转内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论