Java Date Parsing "am" and "

Using SimpleDateFormat, how can I parse the line: "2013-05-23T09: 18: 07 pm.380 + 0000"

All of my SimpleDateFormat lines are at odds with the "page" part .

Thanks in advance.

EDIT: We have no control over the incoming format.

I tried:

"yyyy-MM-dd'T'HH:mm:ss aa.SSSZ" "yyyy-MM-dd'T'HH:mm:ss aaaa.SSSZ" "yyyy-MM-dd'T'HH:mm:ss a.'m'..SSSZ" "yyyy-MM-dd'T'HH:mm:ss a.'m.'.SSSZ" "yyyy-MM-dd'T'HH:mm:ss a.'m..'SSSZ" "yyyy-MM-dd'T'HH:mm:ss aa'm'..SSSZ" "yyyy-MM-dd'T'HH:mm:ss aa'm.'.SSSZ" "yyyy-MM-dd'T'HH:mm:ss aaa'..'SSSZ" "yyyy-MM-dd'T'HH:mm:ss aaa.'.'SSSZ" "yyyy-MM-dd'T'HH:mm:ss aaa'.'.SSSZ" 
+4
source share
4 answers

It's not clear what “380 + 0000” means, but you can fix the AM / PM part by setting DateFormatSymbols to SimpleDateFormat . Here is an example:

 import java.util.*; import java.text.*; public class Test { public static void main(String[] args) throws Exception { String text = "2013-05-23T09:18:07 pm.380+0000"; String pattern = "yyyy-MM-dd'T'hh:mm:ss aa'.380+0000'"; SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.US); format.setTimeZone(TimeZone.getTimeZone("UTC")); DateFormatSymbols symbols = format.getDateFormatSymbols(); symbols = (DateFormatSymbols) symbols.clone(); symbols.setAmPmStrings(new String[] { "am", "pm"}); format.setDateFormatSymbols(symbols); Date date = format.parse(text); System.out.println(date); } } 

I don't know if DateFormatSymbols need to be cloned before mutating it - this is not clear, to be honest ... the documentation points out two ways

DateFormatSymbols objects are clonable. When you get a DateFormatSymbols object, feel free to change the date and time formatting data. For example, you can replace the localized characters of a date and time pattern with ones that you can easily remember. Or you can change representative cities to your favorites.

Given that he mentions cloning, this suggests that you should clone, but the next paragraph does not say: (

+13
source

Here's the implementation using Java 8 of the new java.time package, so you can cut out java.util.Date and java.text.SimpleDateFormat :

 import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; import java.util.HashMap; import java.util.Map; public class Test { public static void main(String[] args) { String text = "2013-05-23T09:18:07 pm.380+0000"; Map<Long, String> map = new HashMap<>(); map.put(0L, "am"); map.put(1L, "pm"); DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendPattern("yyyy-MM-dd'T'hh:mm:ss ") .appendText(ChronoField.AMPM_OF_DAY, map) .appendPattern(".SSSZ").toFormatter(); OffsetDateTime dateTime = OffsetDateTime.parse(text, formatter); System.out.println(dateTime); } } 

Launch:

 2013-05-23T21:18:07.380Z 
+2
source

Just delete the m. partly through string manipulation. This is redundant.

Instead of simple parsing SimpleDateFormat.

Something like that:

 String whatever = "2013-05-23T09:18:07 pm.380+0000"; whatever = whatever.replaceAll(" pm.", ":").replaceAll(" am.", ":"); System.out.println(whatever); S String pattern = "yyyy-MM-dd'T'hh:mm:ss:SSS'Z'"; SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.US); Date date; try { date = format.parse(whatever); System.out.println(date); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

Update

As indicated by the IAN, I missed that the watch is not in a 24-hour format. However, I received part of a millisecond and added part of a time zone to John Skeet's answer to get:

 String text = "2013-05-23T09:18:07 pm.380+0000"; String pattern = "yyyy-MM-dd'T'hh:mm:ss aa'.'SSSZ"; SimpleDateFormat format = new SimpleDateFormat(pattern); DateFormatSymbols symbols = format.getDateFormatSymbols(); symbols = (DateFormatSymbols) symbols.clone(); symbols.setAmPmStrings(new String[] { "am", "pm"}); format.setDateFormatSymbols(symbols); Date date = format.parse(text); System.out.println(date); 
0
source

Do it like this:

 String time="2013-05-23T09:18:07 pm.380+0000"; int index1=time.indexOf(""); int index2 = time.indexOf(".."); // or you can give pm as argument if you want it to stop before pm String result = time.substring(index1,index2); System.out.print(result); 

This has the following result:

 2013-05-23T09:18:07 pm 

I hope this helps

0
source

Source: https://habr.com/ru/post/1482472/


All Articles