2020-04-03 20:17:46 转换为 “yyyy-MM-dd’T’HH:mm:ss” 格式

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

2020-04-03 20:17:46 to "yyyy-MM-dd'T'HH:mm:ss" format

问题

在Java中(java.util.*或Joda API),是否有将"2020-04-03 20:17:46"转换为"yyyy-MM-dd'T'HH:mm:ss"的方法?

  1. new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
  2. .parse("2020-04-03 20:17:46")

它始终会抛出java.text.parseException。

英文:

Is there any way in java(java.util.* or Joda api ) to convert "2020-04-03 20:17:46" to "yyyy-MM-dd'T'HH:mm:ss"

  1. new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
  2. .parse("2020-04-03 20:17:46")

its giving java.text.parseException always

答案1

得分: 4

只针对您使用的是Java 8或更高版本的情况下,可使用java.time。以下是一个简单的示例:

  1. public static void main(String[] args) {
  2. // 示例日期时间
  3. String datetime = "2020-04-03 20:17:46";
  4. // 创建一个解析此模式日期时间的格式化程序
  5. DateTimeFormatter parserDtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  6. // 使用该格式化程序解析日期时间
  7. LocalDateTime ldt = LocalDateTime.parse(datetime, parserDtf);
  8. // 为了输出已解析的日期时间,使用默认格式化程序(隐式)
  9. System.out.println(ldt);
  10. // 或者以完全不同的方式对其进行格式化
  11. System.out.println(ldt.format(
  12. DateTimeFormatter.ofPattern("EEE, dd. 'of' MMM 'at' hh-mm-ss a",
  13. Locale.ENGLISH)
  14. )
  15. );
  16. }

这将输出

  1. 2020-04-03T20:17:46
  2. Fri, 03. of Apr at 08-17-46 PM

请注意,这不考虑任何时区或偏移量,它只表示由传递或解析的年份、月份、日期、小时、分钟和秒组成的日期和时间,没有其他内容。

英文:

Just for the case you are using Java 8 or above, make use of java.time.
See this simple example:

  1. public static void main(String[] args) {
  2. // example datetime
  3. String datetime = "2020-04-03 20:17:46";
  4. // create a formatter that parses datetimes of this pattern
  5. DateTimeFormatter parserDtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  6. // then parse the datetime with that formatter
  7. LocalDateTime ldt = LocalDateTime.parse(datetime, parserDtf);
  8. // in order to output the parsed datetime, use the default formatter (implicitly)
  9. System.out.println(ldt);
  10. // or format it in a totally different way
  11. System.out.println(ldt.format(
  12. DateTimeFormatter.ofPattern("EEE, dd. 'of' MMM 'at' hh-mm-ss a",
  13. Locale.ENGLISH)
  14. )
  15. );
  16. }

This outputs

  1. 2020-04-03T20:17:46
  2. Fri, 03. of Apr at 08-17-46 PM

Please note that this doesn't consider any time zone or offset, it just represents a date and time consisting of the passed or parsed years, months, days, hours, minutes and seconds, nothing else.

答案2

得分: 3

不要使用 java.util.* 中的日期/时间 API,因为它们中的大多数现在已经过时。请改用 java.time API。

  1. import java.io.IOException;
  2. import java.time.LocalDateTime;
  3. import java.time.format.DateTimeFormatter;
  4. public class Main {
  5. public static void main(String[] args) throws IOException {
  6. String strDatetime = "2020-04-03 20:17:46";
  7. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  8. LocalDateTime parsedDate = LocalDateTime.parse(strDatetime, formatter);
  9. System.out.println(parsedDate);
  10. }
  11. }

输出:

  1. 2020-04-03T20:17:46

了解更多关于 DateTimeFormatter 的信息,请访问 https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

英文:

Do not use Date/Time API from java.util.* as most of them are now outdated. Use java.time API instead.

  1. import java.io.IOException;
  2. import java.time.LocalDateTime;
  3. import java.time.format.DateTimeFormatter;
  4. public class Main {
  5. public static void main(String[] args) throws IOException {
  6. String strDatetime = "2020-04-03 20:17:46";
  7. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  8. LocalDateTime parsedDate = LocalDateTime.parse(strDatetime, formatter);
  9. System.out.println(parsedDate);
  10. }
  11. }

Output:

  1. 2020-04-03T20:17:46

Learn more about DateTimeFormatter at https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

答案3

得分: 1

这对你有帮助吗?http://tutorials.jenkov.com/java-internationalization/simpledateformat.html

首先,你需要使用旧的格式解析这个字符串,这样你会得到一个日期对象。然后,创建一个新的SimpleDateFormat,使用你的新格式,然后你可以对日期对象进行格式化。

英文:

Could this help you? http://tutorials.jenkov.com/java-internationalization/simpledateformat.html

First you need to parse the String with the old format, you will get a Date object. Then Create a new SimpleDateFormat with your new format, then you can format the Date object.

答案4

得分: 1

  1. String dateString = "2020-04-03 20:17:46";
  2. SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
  3. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  4. Date date = sdf.parse(dateString);
  5. String formattedDate = output.format(date);
英文:
  1. String dateString = "2020-04-03 20:17:46";
  2. SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
  3. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  4. Date date = sdf.parse(dateString);
  5. String formattedDate = output.format(date);

答案5

得分: 0

不是直接这样工作,但如果您仍然想这样做,以下是过程。

使用模式“yyyy-MM-dd HH:mm:ss”创建SimpleDateFormat对象)

使用这个对象解析字符串。最终您将在两种情况下得到日期。对于不包含T的日期,您使用模式中的T是否有特定的原因?

英文:

It do not work that way directly but if you still want to do it then, here is the process.

Create an object of SimpleDateFormat with pattern "yyyy-MM-dd HH:mm:ss")

use this to parse the string. Ultimately you are going to get date in both cases. Is there any specific reason for using T in pattern for dates which do not contain them?

答案6

得分: 0

  1. ## 使用 LocalDateTime
  2. Timestamp timestamp = Timestamp.parse("2020-04-03 20:17:46");
  3. LocalDateTime localDateTime = timestamp.toLocalDateTime();
  4. System.out.println(localDateTime); // 2020-04-03T20:17:46
英文:

Use LocalDateTime.

  1. Timestamp timestamp = Timestamp.parse("2020-04-03 20:17:46");
  2. LocalDateTime localDateTime = timestamp.toLocalDateTime();
  3. System.out.println(localDateTime); // 2020-04-03T20:17:46

huangapple
  • 本文由 发表于 2020年4月9日 03:26:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/61108496.html
匿名

发表评论

匿名网友

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

确定