com.abubusoft.kripton.common.DateUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kripton-core Show documentation
Show all versions of kripton-core Show documentation
Kripton Persistence Library - core module
/*******************************************************************************
* Copyright 2015, 2016 Francesco Benincasa.
*
* 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.abubusoft.kripton.common;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
/**
* The Class DateUtils.
*/
public class DateUtils {
/** The full. */
public static String FULL = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
/** The long. */
public static String LONG = "yyyy-MM-dd HH:mm:ss z";
/** The normal. */
public static String NORMAL = "yyyy-MM-dd z";
/** The short. */
public static String SHORT = "yyyy-MM-dd";
/**
* Read.
*
* @param value the value
* @return the date
*/
public static Date read(String value) {
String pattern = getPattern(value);
Date date=null;
try {
date = ThreadLocalDateFormatter.parse(value, pattern);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* Write.
*
* @param value the value
* @return the string
*/
public static String write(Date value) {
if (value==null) return null;
String text = ThreadLocalDateFormatter.format(value, FULL);
return text;
}
/**
* Write.
*
* @param value the value
* @return the string
*/
public static String writeShort(Date value) {
if (value==null) return null;
String text = ThreadLocalDateFormatter.format(value, SHORT);
return text;
}
/**
* Gets the pattern.
*
* @param text the text
* @return the pattern
*/
static String getPattern(String text) {
int length = text.length();
if (length > 23) {
return FULL;
}
if (length > 20) {
return LONG;
}
if (length > 11) {
return NORMAL;
}
return SHORT;
}
/**
* The Class ThreadLocalDateFormatter.
*/
public static class ThreadLocalDateFormatter {
/** The Constant FORMATTERS. */
private static final ThreadLocal