关于Java中的日期格式化

huangapple go评论107阅读模式
英文:

Regarding date format in java

问题

  1. public class DatePgm {
  2. public static void main(String[] args) throws ParseException {
  3. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss+SS:SZ");
  4. Date date1 = sdf.parse("2020-07-26T18:52:24+05:30");
  5. System.out.println("DATE="+sdf.format(date1));
  6. }
  7. }

请注意,我已经将您提供的代码中的HTML实体代码(如")替换为原始的引号字符。

英文:
  1. public class DatePgm {
  2. public static void main(String[] args) throws ParseException {
  3. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+'SS:SZ");
  4. Date date1 = sdf.parse("2020-07-26T18:52:24+05:30");
  5. System.out.println("DATE="+sdf.format(date1));
  6. }
  7. }

Can any one help me to print the exact date and time format "2020-07-26T18:52:24+05:30"

答案1

得分: 2

tl;dr ⇒ java.time.OffsetDateTime

In this case, you don't really need to define a format/formatter yourself. The default DateTimeFormatter of an OffsetDateTime (which appears suitable here) is able to parse (and print/format) your example String. You can do it as follows:

  1. public static void main(String[] args) {
  2. // your example datetime String
  3. String datetime = "2020-07-26T18:52:24+05:30";
  4. // parse the String to an OffsetDateTime using the default formatter
  5. OffsetDateTime odt = OffsetDateTime.parse(datetime);
  6. // and print the OffsetDateTime using its default formatter
  7. System.out.println("DATE=" + odt);
  8. }

the output is

  1. DATE=2020-07-26T18:52:24+05:30
英文:

tl;dr ⇒ java.time.OffsetDateTime

In this case, you don't really need to define a format/formatter yourself. The default DateTimeFormatter of an OffsetDateTime (which appears suitable here) is able to parse (and print/format) your example String. You can do it as follows:

  1. public static void main(String[] args) {
  2. // your example datetime String
  3. String datetime = "2020-07-26T18:52:24+05:30";
  4. // parse the String to an OffsetDateTime using the default formatter
  5. OffsetDateTime odt = OffsetDateTime.parse(datetime);
  6. // and print the OffsetDateTime using its default formatter
  7. System.out.println("DATE=" + odt);
  8. }

the output is

  1. DATE=2020-07-26T18:52:24+05:30

答案2

得分: 1

使用Instant而不是DateInstant支持带有时区的日期时间。

  1. Instant date1 = Instant.parse("2020-07-26T18:52:24+05:30");
  2. System.out.println("DATE= " + date1);
英文:

Use Instant instead of Date. Instant supports date time with zone

  1. Instant date1 = Instant.parse("2020-07-26T18:52:24+05:30");
  2. System.out.println("DATE= " + date1);

答案3

得分: 1

你可以尝试以下代码:

  1. String string = "2020-07-26T18:52:24+05:30";
  2. OffsetDateTime odt = OffsetDateTime.parse(string);
  3. System.out.println(odt);
英文:

You can try following,

  1. String string = "2020-07-26T18:52:24+05:30";
  2. OffsetDateTime odt = OffsetDateTime.parse( string );
  3. System.out.println(odt);

答案4

得分: 0

你可以使用 Calendar 来获取日期:

  1. String time = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+SS:SZ'").format(Calendar.getInstance().getTime());
英文:

You can use Calendar to get the date:

  1. String time = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+'SS:SZ").format(Calendar.getInstance().getTime());

答案5

得分: 0

我认为你应该使用LocalDateTime,来自Java 8。大多数格式的很好示例都可以在这里找到。

英文:

I think you should use LocalDatetIme, from java 8. A very good example for most of the formats are available here

答案6

得分: 0

使用SimpleDateFormat,您需要设置时区(Asia/Colombo为+05:30):

  1. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
  2. Date date1 = sdf.parse("2020-07-26T18:52:24+05:30");
  3. sdf.setTimeZone(TimeZone.getTimeZone("Asia/Colombo"));
  4. System.out.println("DATE=" + sdf.format(date1));

输出结果:

  1. DATE=2020-07-26T18:52:24+05:30
英文:

With SimpleDateFormat you have to set the timzone(Asia/Colombo is +05:30):

  1. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
  2. Date date1 = sdf.parse("2020-07-26T18:52:24+05:30");
  3. sdf.setTimeZone(TimeZone.getTimeZone("Asia/Colombo"));
  4. System.out.println("DATE="+sdf.format(date1));

Output:

DATE=2020-07-26T18:52:24+05:30

huangapple
  • 本文由 发表于 2020年7月31日 15:36:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63187671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定