如何将CDT格式的时间戳转换为Java中的MM/DD?

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

How to convert Timestamp in CDT Format to MM/DD in Java?

问题

ZonedDateTime ztime = ZonedDateTime.parse("2023-06-05T09:00:01CDT", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz"));
return ztime.format(DateTimeFormatter.ofPattern("M/d"));
英文:

I have a timestamp in cdt format as below "2023-06-05T09:00:01CDT", I want to convert this timestamp into MM/DD format in Java.

I have tried below :
The expected output was: 6/5
But was getting parsing error.

ZonedDateTime ztime = ZonedDateTime.parse("2023-06-05T09:00:01CDT");
Date date = Date.from(Instant.from(ztime));
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);

Stack trace:

Exception in thread "main" java.time.format.DateTimeParseException: Text '2023-06-05T09:00:01CDT' could not be parsed at index 19
	at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2056)
	at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1958)
	at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:600)
	at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:585)
	at MyClass.main(MyClass.java:9)

答案1

得分: 4

如果您只想解析包含时区(三个字母)而不是标准区域标识的完整 ZonedDateTime 字符串表示形式,您需要为其准备一个格式化程序。

成功解析该字符串后,获取 "MM/dd" 格式的字符串的最快方法是使用 ZonedDateTimeformat(DateTimeFormatter) 方法,并定义模式以仅输出年份的月份和日期…

例子代码

public static void main(String[] args) {
    // 示例输入
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssz");
    // 准备一个实际可以解析带有时区的字符串的格式化程序
    ZonedDateTime ztime = ZonedDateTime.parse("2023-06-05T09:00:01CDT", dtf);
    // 打印完整的带时区的日期时间
    System.out.println(ztime);
    // 并以仅考虑年份的月份和日期的格式打印结果
    System.out.println(ztime.format(DateTimeFormatter.ofPattern("M/d")));
}

输出

2023-06-05T09:00:01-05:00[America/Chicago]
6/5

优点

  • 非常简短的解决方案
  • 仅使用一个API,这是现代的方式…
英文:

If you simply want to parse a full ZonedDateTime String representation that includes a time zone (three letters) instead of a standard zone id, you will have to prepare a formatter for it.

Having successfully parsed that String, the quickest way to get a String of the format "MM/dd" would be to use the method format(DateTimeFormatter) of ZonedDateTime and define the pattern to only output month-of-year and day-of-month…

example code

public static void main(String[] args) {
	// example input
	DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssz");
	// prepare a formatter that can actually parse the String with a time zone
	ZonedDateTime ztime = ZonedDateTime.parse("2023-06-05T09:00:01CDT", dtf);
	// print the full zoned date time
	System.out.println(ztime);
	// and print the result in a format that only considers month of year and day of month
	System.out.println(ztime.format(DateTimeFormatter.ofPattern("M/d")));
}

Output

2023-06-05T09:00:01-05:00[America/Chicago]
6/5

Advantages

  • very short solution
  • uses only one API, the modern one…

答案2

得分: 2

这可能会有所帮助。

        String cdtTimestamp = "2023-06-01T15:30:00-05:00"; // 示例CDT时间戳
        // 解析CDT时间戳为ZonedDateTime
        ZonedDateTime cdtDateTime = ZonedDateTime.parse(cdtTimestamp);
        // 转换为本地时区(可选)
        ZonedDateTime localDateTime = cdtDateTime.withZoneSameInstant(ZoneId.systemDefault());
        // 将日期格式化为MM/DD
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MM/dd");
        String formattedDate = localDateTime.format(dateFormatter);
        System.out.println("格式化后的日期:" + formattedDate);
英文:

This might be helpful.

        String cdtTimestamp = "2023-06-01T15:30:00-05:00"; // Example CDT timestamp
        // Parse CDT timestamp into ZonedDateTime
        ZonedDateTime cdtDateTime = ZonedDateTime.parse(cdtTimestamp);
        // Convert to local timezone (optional)
        ZonedDateTime localDateTime = cdtDateTime.withZoneSameInstant(ZoneId.systemDefault());
        // Format the date as MM/DD
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MM/dd");
        String formattedDate = localDateTime.format(dateFormatter);
        System.out.println("Formatted date: " + formattedDate);

答案3

得分: 1

尝试这段代码片段,如果是解析问题,你可以添加一个DateTimeFormatter :: documentation

DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz");
ZonedDateTime time = ZonedDateTime.parse("2023-06-05T09:00:01CDT", format);
Date date = Date.from(Instant.from(time));
Calendar cal = Calendar.getInstance();
cal.setTime(date);
String dateStr = (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
英文:

Try this code snippet, if it is a parsing issue you can add a DateTimeFormatter :: documentation

    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz");
    ZonedDateTime time = ZonedDateTime.parse("2023-06-05T09:00:01CDT", format);
    Date date = Date.from(Instant.from(time));
    cal.setTime(date);
    Calendar cal = Calendar.getInstance();
    String date =  (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);

huangapple
  • 本文由 发表于 2023年6月5日 20:55:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406631.html
匿名

发表评论

匿名网友

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

确定