parsing date and time in java
I have Time format in (yyyy-mm-ddTHH:mm+0:0000)..i need to convert this 开发者_开发知识库format to mm-dd-yyyy hh:mm am/pm using java program...any one help me to do this..thanx in advance...
Try something like this:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm+s:SSSS");
Date date = format.parse(dateString);
format = new SimpleDateFormat("MM-dd-yyyy hh:mm a");
dateString = format.format(date);
Here’s the modern answer. SimpleDateFormat
was the right answer in 2011 when the question was asked, but it isn’t any longer. The old classes turned out to be troublesome, so at long last their replacements came out early in 2014.
I was a bit puzzled about a couple of details in your formats.
yyyy-mm-ddTHH:mm+0:0000
looks a bit like an ISO 8601 offset date-time, but then+0:0000
should be the offset from UTC. I am speculating that it may have been intended as an offset inh:mmss
format, but it is non-standard and nothing I have ever seen elsewhere.- You seem to be asking for am/pm in lowercase. This is non-standard too and not built-in with the standard classes.
I suggest:
DateTimeFormatter sourceFormat
= DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm'+0:0000'");
DateTimeFormatter targetFormat
= DateTimeFormatter.ofPattern("MM-dd-uuuu hh:mm a", Locale.ENGLISH);
String sourceTime = "2017-07-05T20:31+0:0000";
String convertedTime = LocalDateTime.parse(sourceTime, sourceFormat)
.format(targetFormat)
.toLowerCase(Locale.ENGLISH);
System.out.println(convertedTime);
This prints
07-05-2017 08:31 pm
I found it safest just to require literal +0:0000
in the input, not trying to interpret it. For producing pm
in lowercase, I converted the whole string to lowercase after formatting. There is a different trick in this answer.
An attempt to be more flexible with hours, minutes and seconds in the offset and make sense of them could be:
sourceTime = sourceTime.replaceFirst("(\\d:\\d{2})(\\d{2})$", "0$1:$2");
String convertedTime = OffsetDateTime.parse(sourceTime)
.format(targetFormat)
.toLowerCase(Locale.ENGLISH);
The replaceFirst
converts 0:0000
to 00:00:00
, which when preceeded by +
or -
conforms with an ISO 8601 offset. This will work with other offsets too (as long as hour is one digit; you may want to try both one and two digits in turn). In the concrete code we are still ignoring the offset when formatting, but it could be used for other purposes if desired.
DateTimeFormatter
, LocalDateTime
and OffsetDateTime
are part of JSR-310. They are built in in Java 8 and later. You may use them in Java 6 and 7 too through the ThreeTen Backport. There is also a specific edition of the backport for Android, ThreeTenABP.
Look at java.text.SimpleDateFormat
- you'll need two instances, one for parsing, one for formatting.
See the working example with this code snippet:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTimeAMPM {
public static void main(String[] args) throws ParseException {
DateFormat inputDateFormatter = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm+s:SSSS");
Date date = inputDateFormatter.parse("2011-11-11T22:33+0:400");
String outputDateFormatter = "MM-dd-yyyy HH:mm:ss a";
SimpleDateFormat sdf = new SimpleDateFormat(outputDateFormatter);
System.out.println("Date: " + sdf.format(date));
}
}
You can do what Paulo and morja say or just use String.substring() since your parsing-requirements are very simple.
You should create Data object from your String -> use appropriate class (e.g. SimpleDataFormat) and then print it by using another format.
精彩评论