How to convert String Jul 21, 2020, 7:53:14 PM into "DD/MM/YYYY a" format in java

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

How to convert String Jul 21, 2020, 7:53:14 PM into "DD/MM/YYYY a" format in java

问题

I have a string as "Jul 22, 2020, 11:56:51 PM" and I want to convert it into "DD/MM/YYYY a" format so that the output is "22/07/2020 11:56:51 PM".

I have tried but it gives me a time difference of 5:30 hours. I want to have the same date.

ExportObj has a property date which is of type String.

Output I'm getting is - 07/22/2020 06:26 PM

public class Export {

    public static void main(String[] args) {
        SimpleDateFormat sf = new SimpleDateFormat(user.getDateFormatWithTime());
        String id = String.valueOf(currentUser.getTimeZoneId());
        TimeZone timeZone = TimeZone.getTimeZone(id);
        sf.setTimeZone(timeZone);
        ExportObj obj = new ExportObj();
        obj.setDate(sf.format(new Date()));
        System.out.println(obj.getDate());
    }
}
英文:

I have a string as "Jul 22, 2020, 11:56:51 PM" and I want to convert it into "DD/MM/YYYY a" format so that the output is 22/07/2020 11:56:51 PM.

I have tried but it gives me a time difference of 5:30 hours. I want to have the same date.

> ExportObj has a property date which is of type String.

Output I'm getting is - 07/22/2020 06:26 PM

public Class Export {

public static void main(string[] args){
        SimpleDateFormat sf = new SimpleDateFormat(user.getDateFormatWithTime());
        String id = String.valueOf(currentUser.getTimeZoneId());
        TimeZone timeZone = TimeZone.getTimeZone(id);
        sf.setTimeZone(timeZone);
        ExportObj obj = new ExportObj();
        obj.setDate(sf.format(new Date()));
        System.out.println(obj.getDate());
  }
}

答案1

得分: 1

不应将日期和时间作为 String 存储在您的 ExportObj 中。就像您将数字存储在 intdouble 变量中,将布尔值存储在 boolean 变量中一样(我希望如此),请使用适当的日期时间类型来表示日期和时间。在这种情况下,ZonedDateTime 可能最适合,但根据具体情况和精确要求,您可以考虑使用 Instant 或来自 java.time(现代Java日期和时间API)的其他类。

当您需要向用户显示日期和时间时,请将日期时间格式化为适合该目的的字符串。

java.time

对于日期和时间操作,请使用 java.time,这是现代的Java日期和时间API。对于您的转换:

DateTimeFormatter sourceFormatter = DateTimeFormatter.ofPattern("MMM d, uuuu, hh:mm:ss a", Locale.ENGLISH);
DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu hh:mm:ss a", Locale.ENGLISH);

String id = "Etc/UTC";
String stringWeveGot = "Jul 22, 2020, 11:56:51 PM";

ZoneId zone = ZoneId.of(id);
ZonedDateTime dateTime = LocalDateTime.parse(stringWeveGot, sourceFormatter)
        .atZone(zone);

System.out.println(dateTime);

String convertedString = dateTime.format(targetFormatter);

System.out.println(convertedString);

输出:

> 2020-07-22T23:56:51Z[Etc/UTC]
> 22/07/2020 11:56:51 PM

正如我所说,请将 ZonedDateTime 对象放入您的 ExportObj 对象中,而不是字符串。

在您的代码中出了什么问题?

很难确定发生了什么问题,因为您的代码不完整,我无法运行它。您可能只将时区应用于其中一个格式化程序,而另一个格式化程序可能使用不同的时区,例如您的JVM的默认时区,导致不希望的时区转换。java.timeZonedDateTime 更容易处理此情况,因为与老式的 Date 类不同,它将时区保存在日期时间对象内部,而不是在格式化程序中。

链接

英文:

You should not keep date and time as a String in your ExportObj. Just as you keep numbers in int and double variables and Boolean values in boolean variables (I hope), use proper date-time types for your dates and times. In this case a ZonedDateTime is probably best suited, but depending on circumstances and precise requirements you may consider Instant or some other class from java.time, the modern Java date and time API.

When you need to show the date and time to a user, format the date-time into an appropriate string for that purpose.

java.time

Use java.time, the modern Java date and time API, for your date and time work. For your conversion:

	DateTimeFormatter sourceFormatter = DateTimeFormatter.ofPattern("MMM d, uuuu, hh:mm:ss a", Locale.ENGLISH);
	DateTimeFormatter targetormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu hh:mm:ss a", Locale.ENGLISH);
	
	String id = "Etc/UTC";
	String stringWeveGot = "Jul 22, 2020, 11:56:51 PM";
	
	ZoneId zone = ZoneId.of(id);
	ZonedDateTime dateTime = LocalDateTime.parse(stringWeveGot, sourceFormatter)
			.atZone(zone);
	
	System.out.println(dateTime);
	
	String convertedString = dateTime.format(targetormatter);
	
	System.out.println(convertedString);

Output:

> 2020-07-22T23:56:51Z[Etc/UTC]
> 22/07/2020 11:56:51 PM

As I said, put the ZonedDateTime object into your ExportObj object, not the string.

What went wrong in your code?

It’s a bit hard for me to be sure what happened when you got a time that was 5 hours 30 minutes too early since your code isn’t complete and I cannot run it. You probably applied your time zone to only one of your formatters, leaving the other one to use a different time zone, for example the default time zone of your JVM, giving rise to an unwanted conversion between those time zones. ZonedDateTime of java.time makes this situation easier to handle since contrary to the old-fashioned Date class it keeps the time zone inside the date-time object rather than in the formatters.

答案2

得分: 0

public class Test {

    public static void main(String[] args) throws ParseException {

        final String OLD_FORMAT = "MMM dd, yyyy, hh:mm:ss a";
        final String NEW_FORMAT = "dd/MM/yyyy hh:mm:ss a";

        String oldDateString = "Jul 22, 2020, 11:56:51 PM";
        String newDateString;

        SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
        Date d = sdf.parse(oldDateString);
        sdf.applyPattern(NEW_FORMAT);
        newDateString = sdf.format(d);
        System.out.println(newDateString);

    }
}

Output:

22/07/2020 11:56:51 PM
英文:

Run this :

public class Test {

	public static void main(String [] args) throws ParseException {

		
		final String OLD_FORMAT = "MMM dd, yyyy, hh:mm:ss a";
		final String NEW_FORMAT = "dd/MM/yyyy hh:mm:ss a";

		String oldDateString = "Jul 22, 2020, 11:56:51 PM";
		String newDateString;

		SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
		Date d = sdf.parse(oldDateString);
		sdf.applyPattern(NEW_FORMAT);
		newDateString = sdf.format(d);
		System.out.println(newDateString);
		
	}

Output :

22/07/2020 11:56:51 PM

huangapple
  • 本文由 发表于 2020年7月23日 02:39:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63041069.html
匿名

发表评论

匿名网友

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

确定