关于Java中的日期格式化

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

Regarding date format in java

问题

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

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

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

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:

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

the output is

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:

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

the output is

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

答案2

得分: 1

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

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

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

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

答案3

得分: 1

你可以尝试以下代码:

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

You can try following,

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

答案4

得分: 0

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

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:

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):

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

输出结果:

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

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

  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
  Date date1 = sdf.parse("2020-07-26T18:52:24+05:30");
  sdf.setTimeZone(TimeZone.getTimeZone("Asia/Colombo"));
  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:

确定