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

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

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

问题

  1. ZonedDateTime ztime = ZonedDateTime.parse("2023-06-05T09:00:01CDT", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz"));
  2. 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.

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

Stack trace:

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

答案1

得分: 4

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

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

例子代码

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

输出

  1. 2023-06-05T09:00:01-05:00[America/Chicago]
  2. 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

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

Output

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

Advantages

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

答案2

得分: 2

这可能会有所帮助。

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

This might be helpful.

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

答案3

得分: 1

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

  1. DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz");
  2. ZonedDateTime time = ZonedDateTime.parse("2023-06-05T09:00:01CDT", format);
  3. Date date = Date.from(Instant.from(time));
  4. Calendar cal = Calendar.getInstance();
  5. cal.setTime(date);
  6. 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

  1. DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz");
  2. ZonedDateTime time = ZonedDateTime.parse("2023-06-05T09:00:01CDT", format);
  3. Date date = Date.from(Instant.from(time));
  4. cal.setTime(date);
  5. Calendar cal = Calendar.getInstance();
  6. 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:

确定