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

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

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"的方法?

new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
          .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"

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

its giving java.text.parseException always

答案1

得分: 4

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

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

这将输出

2020-04-03T20:17:46
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:

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

This outputs

2020-04-03T20:17:46
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。

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {

    public static void main(String[] args) throws IOException {
        String strDatetime = "2020-04-03 20:17:46";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parsedDate = LocalDateTime.parse(strDatetime, formatter);
        System.out.println(parsedDate);
    }
}

输出:

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.

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {

	public static void main(String[] args) throws IOException {
		String strDatetime = "2020-04-03 20:17:46";
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime parsedDate = LocalDateTime.parse(strDatetime, formatter);
		System.out.println(parsedDate);
	}
}

Output:

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

String dateString = "2020-04-03 20:17:46";
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(dateString);
String formattedDate = output.format(date);
英文:
    String dateString = "2020-04-03 20:17:46";
    SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = sdf.parse(dateString);
    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

## 使用 LocalDateTime

        Timestamp timestamp = Timestamp.parse("2020-04-03 20:17:46");
  
        LocalDateTime localDateTime = timestamp.toLocalDateTime();  

        System.out.println(localDateTime); // 2020-04-03T20:17:46
英文:

Use LocalDateTime.

    Timestamp timestamp = Timestamp.parse("2020-04-03 20:17:46");

    LocalDateTime localDateTime = timestamp.toLocalDateTime();  

    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:

确定