开发者

How to convert time data in java?

开发者 https://www.devze.com 2023-01-09 21:31 出处:网络
The following time formats are in outlook calendar file DTSTART;TZID=\"Eastern\":20100728T140000 DTEND;TZID=\"Eastern\":20100728T150000

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

精彩评论

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