简体   繁体   中英

parsing date and time in java

我有时间格式(yyyy-mm-ddTHH:mm + 0:0000)..我需要将这种格式转换为mm-dd-yyyy hh:mm am / pm使用java程序...任何人帮我做这个...提前...

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 in h: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 .

看看java.text.SimpleDateFormat - 你需要两个实例,一个用于解析,一个用于格式化。

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));
}

}

您可以执行Paulo和morja所说的或仅使用String.substring(),因为您的解析要求非常简单。

您应该从String中创建Data对象 - >使用适当的类(例如SimpleDataFormat ),然后使用其他格式打印它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM