开发者

How to convert time data in java?

The following time formats are in outlook calendar file

DTSTART;TZID="Eastern":20100728T140000
DTEND;TZID="Eastern":20100728T150000

how to convert t开发者_高级运维his time to java time format.


This looks like iCalendar. Take a look at ical4j - a Java API for it.


Without tested, look at SimpleDateFormat

String ds = "DTSTART;TZID=\"Eastern\":20100728T140000";
Date d = new SimpleDateFormat("yyyyMMdd'T'HHMMSS").parse(ds.split(":")[1]);


Handling the timezone will be tricky as "Eastern" is not an actual timezone. However if you handle that, I would suggest the following SimpleDateFormat will handle the unadjusted parse for you.

Date unadjusted = 
    new SimpleDateFormat("yyyyMMdd'T'HHmmss").parse(line.split(":")[1]);


Another way using always SimpleDateFormat:

String[] strings = new String[]{"DTSTART;TZID=\"Eastern\":20100728T140000", "DTEND;TZID=\"Eastern\":20100728T150000"};

    for (String string : strings) {
        String dateString = string.replaceAll("(DTSTART|DTEND);TZID=\"Eastern\":", "");
        dateString = dateString.replaceAll("T", "");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            Date date = sdf.parse(dateString);
            System.out.println(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜