
com.yandex.disk.rest.util.ISO8601 Maven / Gradle / Ivy
/*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yandex.disk.rest.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class ISO8601 {
private static final String FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
public static String fromCalendar(final Calendar calendar) {
Date date = calendar.getTime();
String formatted = new SimpleDateFormat(FORMAT).format(date);
return formatted.substring(0, 22) + ":" + formatted.substring(22);
}
public static Calendar toCalendar(final String iso8601string)
throws ParseException {
Calendar calendar = GregorianCalendar.getInstance();
String s = iso8601string.replace("Z", "+00:00");
try {
s = s.substring(0, 22) + s.substring(23);
} catch (IndexOutOfBoundsException e) {
throw new ParseException("Invalid length", 0);
}
Date date = new SimpleDateFormat(FORMAT).parse(s);
calendar.setTime(date);
return calendar;
}
public static Date parse(final String iso8601string) {
try {
return toCalendar(iso8601string).getTime();
} catch (ParseException e) {
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy