
com.baidu.mochow.util.DateUtils Maven / Gradle / Ivy
/*
* Copyright 2024 Baidu, Inc.
*
* 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.baidu.mochow.util;
import java.util.Date;
import java.util.Locale;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
/**
* Utilities for parsing and formatting dates.
*
* Note that this class doesn't use static methods because of the synchronization issues with SimpleDateFormat. This
* lets synchronization be done on a per-object level, instead of on a per-class level.
*/
public class DateUtils {
/**
* Alternate ISO 8601 format without fractional seconds
*/
private static final DateTimeFormatter ALTERNATE_ISO8601_DATE_FORMAT =
ISODateTimeFormat.dateTimeNoMillis().withZone(DateTimeZone.UTC);
/**
* RFC 822 format
*/
private static final DateTimeFormatter RFC822_DATE_FORMAT =
DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'").withLocale(Locale.US).withZone(DateTimeZone.UTC);
/**
* Formats the specified date as an ISO 8601 string.
*
* @param date The date to format.
* @return The ISO 8601 string representing the specified date.
*/
public static String formatAlternateIso8601Date(Date date) {
return DateUtils.ALTERNATE_ISO8601_DATE_FORMAT.print(new DateTime(date));
}
/**
* Formats the specified date as an RFC 822 string.
*
* @param date The date to format.
* @return The RFC 822 string representing the specified date.
*/
public static String formatRfc822Date(Date date) {
return DateUtils.RFC822_DATE_FORMAT.print(new DateTime(date));
}
}