用Java解析javatdate格式
如何解析JavaTDate?我想将其解析为一个java日期对象。日期值为2011-11-22T00:00:00-06:00您可以使用: 就个人而言,我强烈建议您使用而不是内置Java类型,但这是一个单独的讨论…您可以使用: 就我个人而言,我强烈建议您使用内置Java类型,但这是一个单独的讨论…Java.time用Java解析javatdate格式,java,parsing,date,Java,Parsing,Date,如何解析JavaTDate?我想将其解析为一个java日期对象。日期值为2011-11-22T00:00:00-06:00您可以使用: 就个人而言,我强烈建议您使用而不是内置Java类型,但这是一个单独的讨论…您可以使用: 就我个人而言,我强烈建议您使用内置Java类型,但这是一个单独的讨论…Java.time java.utilDate-Time API及其格式化APISimpleDataFormat已经过时,并且容易出错。建议完全停止使用,并切换到2014年3月发布的* 使用java.tim
java.util
Date-Time API及其格式化APISimpleDataFormat
已经过时,并且容易出错。建议完全停止使用,并切换到2014年3月发布的*
使用java.time
,现代日期时间API的解决方案:
现代日期时间API基于并且不需要显式使用DateTimeFormatter
对象,只要日期时间字符串符合ISO 8601标准
由于您的日期时间字符串,2011-11-22T00:00:00-06:00
已经符合ISO 8601日期时间格式和时区偏移,您可以直接将其解析为OffsetDateTime
演示:
import java.time.OffsetDateTime;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.parse("2011-11-22T00:00:00-06:00");
System.out.println(odt);
}
}
2011-11-22T00:00-06:00
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("y-M-d'T'H:m:sXXX", Locale.ENGLISH);
Date date = sdf.parse("2011-11-22T00:00:00-06:00");
// Display in the default format i.e. the value of date#toString
System.out.println(date);
// #################Display in custom format and timezone#################
// At timezone offset of -06:00 hours
sdf.setTimeZone(TimeZone.getTimeZone("GMT-06:00"));
System.out.println(sdf.format(date));
// In UTC
sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
System.out.println(sdf.format(date));
// In New York
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println(sdf.format(date));
}
}
Tue Nov 22 06:00:00 GMT 2011
2011-11-22T0:0:0-06:00
2011-11-22T6:0:0Z
2011-11-22T1:0:0-05:00
输出:
import java.time.OffsetDateTime;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.parse("2011-11-22T00:00:00-06:00");
System.out.println(odt);
}
}
2011-11-22T00:00-06:00
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("y-M-d'T'H:m:sXXX", Locale.ENGLISH);
Date date = sdf.parse("2011-11-22T00:00:00-06:00");
// Display in the default format i.e. the value of date#toString
System.out.println(date);
// #################Display in custom format and timezone#################
// At timezone offset of -06:00 hours
sdf.setTimeZone(TimeZone.getTimeZone("GMT-06:00"));
System.out.println(sdf.format(date));
// In UTC
sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
System.out.println(sdf.format(date));
// In New York
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println(sdf.format(date));
}
}
Tue Nov 22 06:00:00 GMT 2011
2011-11-22T0:0:0-06:00
2011-11-22T6:0:0Z
2011-11-22T1:0:0-05:00
从了解有关现代日期时间API的更多信息
一些重要注意事项:
import java.time.OffsetDateTime;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.parse("2011-11-22T00:00:00-06:00");
System.out.println(odt);
}
}
2011-11-22T00:00-06:00
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("y-M-d'T'H:m:sXXX", Locale.ENGLISH);
Date date = sdf.parse("2011-11-22T00:00:00-06:00");
// Display in the default format i.e. the value of date#toString
System.out.println(date);
// #################Display in custom format and timezone#################
// At timezone offset of -06:00 hours
sdf.setTimeZone(TimeZone.getTimeZone("GMT-06:00"));
System.out.println(sdf.format(date));
// In UTC
sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
System.out.println(sdf.format(date));
// In New York
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println(sdf.format(date));
}
}
Tue Nov 22 06:00:00 GMT 2011
2011-11-22T0:0:0-06:00
2011-11-22T6:0:0Z
2011-11-22T1:0:0-05:00
OffsetDateTime
转换为具有不同ZoneOffset
的OffsetDateTime
对象,可以使用OffsetDateTime
对象转换为java.util.Date
对象,可以按如下操作:-06:00
,因此正确的解析格式为
y-M-d'T'H:m:sXXX
请注意,对于解析,单个y
可以同时表示年份的两位数和四位数。同样,一个m
可以同时满足一位数和两位数月份的需求。其他字段的行为也类似
演示:
import java.time.OffsetDateTime;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.parse("2011-11-22T00:00:00-06:00");
System.out.println(odt);
}
}
2011-11-22T00:00-06:00
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("y-M-d'T'H:m:sXXX", Locale.ENGLISH);
Date date = sdf.parse("2011-11-22T00:00:00-06:00");
// Display in the default format i.e. the value of date#toString
System.out.println(date);
// #################Display in custom format and timezone#################
// At timezone offset of -06:00 hours
sdf.setTimeZone(TimeZone.getTimeZone("GMT-06:00"));
System.out.println(sdf.format(date));
// In UTC
sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
System.out.println(sdf.format(date));
// In New York
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println(sdf.format(date));
}
}
Tue Nov 22 06:00:00 GMT 2011
2011-11-22T0:0:0-06:00
2011-11-22T6:0:0Z
2011-11-22T1:0:0-05:00
输出:
import java.time.OffsetDateTime;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.parse("2011-11-22T00:00:00-06:00");
System.out.println(odt);
}
}
2011-11-22T00:00-06:00
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("y-M-d'T'H:m:sXXX", Locale.ENGLISH);
Date date = sdf.parse("2011-11-22T00:00:00-06:00");
// Display in the default format i.e. the value of date#toString
System.out.println(date);
// #################Display in custom format and timezone#################
// At timezone offset of -06:00 hours
sdf.setTimeZone(TimeZone.getTimeZone("GMT-06:00"));
System.out.println(sdf.format(date));
// In UTC
sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
System.out.println(sdf.format(date));
// In New York
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println(sdf.format(date));
}
}
Tue Nov 22 06:00:00 GMT 2011
2011-11-22T0:0:0-06:00
2011-11-22T6:0:0Z
2011-11-22T1:0:0-05:00
请注意,java.util.Date
对象不像;相反,它表示自称为“历元”的标准基准时间(即1970年1月1日00:00:00 GMT)起的毫秒数。因为它不保存任何格式和时区信息,所以它应用格式,EEE MMM dd HH:mm:ss z yyyy
和JVM的时区来返回从该毫秒值派生的Date\toString
值。如果需要以不同的格式和时区打印日期和时间,则需要使用SimpleDateFormat
以及所需的格式和适用的时区,如使用上述代码所示
*无论出于何种原因,如果您必须坚持使用Java6或Java7,您可以使用哪个backport将大部分Java.time功能移植到Java6&7。如果您正在为Android项目工作,并且您的Android API级别仍然不符合Java-8,请检查并确认。 java.time
java.util
Date-Time API及其格式化APISimpleDataFormat
已经过时,并且容易出错。建议完全停止使用,并切换到2014年3月发布的*
使用java.time
,现代日期时间API的解决方案:
现代日期时间API基于并且不需要显式使用DateTimeFormatter
对象,只要日期时间字符串符合ISO 8601标准
由于您的日期时间字符串,2011-11-22T00:00:00-06:00
已经符合ISO 8601日期时间格式和时区偏移,您可以直接将其解析为OffsetDateTime
演示:
import java.time.OffsetDateTime;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.parse("2011-11-22T00:00:00-06:00");
System.out.println(odt);
}
}
2011-11-22T00:00-06:00
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("y-M-d'T'H:m:sXXX", Locale.ENGLISH);
Date date = sdf.parse("2011-11-22T00:00:00-06:00");
// Display in the default format i.e. the value of date#toString
System.out.println(date);
// #################Display in custom format and timezone#################
// At timezone offset of -06:00 hours
sdf.setTimeZone(TimeZone.getTimeZone("GMT-06:00"));
System.out.println(sdf.format(date));
// In UTC
sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
System.out.println(sdf.format(date));
// In New York
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println(sdf.format(date));
}
}
Tue Nov 22 06:00:00 GMT 2011
2011-11-22T0:0:0-06:00
2011-11-22T6:0:0Z
2011-11-22T1:0:0-05:00
输出:
import java.time.OffsetDateTime;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.parse("2011-11-22T00:00:00-06:00");
System.out.println(odt);
}
}
2011-11-22T00:00-06:00
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("y-M-d'T'H:m:sXXX", Locale.ENGLISH);
Date date = sdf.parse("2011-11-22T00:00:00-06:00");
// Display in the default format i.e. the value of date#toString
System.out.println(date);
// #################Display in custom format and timezone#################
// At timezone offset of -06:00 hours
sdf.setTimeZone(TimeZone.getTimeZone("GMT-06:00"));
System.out.println(sdf.format(date));
// In UTC
sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
System.out.println(sdf.format(date));
// In New York
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println(sdf.format(date));
}
}
Tue Nov 22 06:00:00 GMT 2011
2011-11-22T0:0:0-06:00
2011-11-22T6:0:0Z
2011-11-22T1:0:0-05:00
从了解有关现代日期时间API的更多信息
一些重要注意事项:
import java.time.OffsetDateTime;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.parse("2011-11-22T00:00:00-06:00");
System.out.println(odt);
}
}
2011-11-22T00:00-06:00
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("y-M-d'T'H:m:sXXX", Locale.ENGLISH);
Date date = sdf.parse("2011-11-22T00:00:00-06:00");
// Display in the default format i.e. the value of date#toString
System.out.println(date);
// #################Display in custom format and timezone#################
// At timezone offset of -06:00 hours
sdf.setTimeZone(TimeZone.getTimeZone("GMT-06:00"));
System.out.println(sdf.format(date));
// In UTC
sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
System.out.println(sdf.format(date));
// In New York
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println(sdf.format(date));
}
}
Tue Nov 22 06:00:00 GMT 2011
2011-11-22T0:0:0-06:00
2011-11-22T6:0:0Z
2011-11-22T1:0:0-05:00
OffsetDateTime
转换为具有不同ZoneOffset
的OffsetDateTime
对象,可以使用OffsetDateTime
对象转换为java.util.Date
对象,可以按如下操作:-06:00
,因此正确的解析格式为
y-M-d'T'H:m:sXXX
请注意,对于解析,单个y
可以同时表示年份的两位数和四位数。同样,一个m
可以同时满足一位数和两位数月份的需求。其他字段的行为也类似
演示:
import java.time.OffsetDateTime;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.parse("2011-11-22T00:00:00-06:00");
System.out.println(odt);
}
}
2011-11-22T00:00-06:00
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("y-M-d'T'H:m:sXXX", Locale.ENGLISH);
Date date = sdf.parse("2011-11-22T00:00:00-06:00");
// Display in the default format i.e. the value of date#toString
System.out.println(date);
// #################Display in custom format and timezone#################
// At timezone offset of -06:00 hours
sdf.setTimeZone(TimeZone.getTimeZone("GMT-06:00"));
System.out.println(sdf.format(date));
// In UTC
sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
System.out.println(sdf.format(date));
// In New York
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println(sdf.format(date));
}
}
Tue Nov 22 06:00:00 GMT 2011
2011-11-22T0:0:0-06:00
2011-11-22T6:0:0Z
2011-11-22T1:0:0-05:00
输出:
import java.time.OffsetDateTime;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.parse("2011-11-22T00:00:00-06:00");
System.out.println(odt);
}
}
2011-11-22T00:00-06:00
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("y-M-d'T'H:m:sXXX", Locale.ENGLISH);
Date date = sdf.parse("2011-11-22T00:00:00-06:00");
// Display in the default format i.e. the value of date#toString
System.out.println(date);
// #################Display in custom format and timezone#################
// At timezone offset of -06:00 hours
sdf.setTimeZone(TimeZone.getTimeZone("GMT-06:00"));
System.out.println(sdf.format(date));
// In UTC
sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
System.out.println(sdf.format(date));
// In New York
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println(sdf.format(date));
}
}
Tue Nov 22 06:00:00 GMT 2011
2011-11-22T0:0:0-06:00
2011-11-22T6:0:0Z
2011-11-22T1:0:0-05:00
请注意,java.util.Date
对象不像;相反,它表示自称为“历元”的标准基准时间(即1970年1月1日00:00:00 GMT)起的毫秒数。因为它不保存任何格式和时区信息,所以它应用格式,EEE MMM dd HH:mm:ss z yyyy
和JVM的时区来返回从该毫秒值派生的Date\toString
值。如果需要以不同的格式和时区打印日期时间,则需要使用SimpleDateFormat