All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.elephantdrummer.parser.ParserDate Maven / Gradle / Ivy

There is a newer version: 1.2.5
Show newest version
package com.elephantdrummer.parser;

import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import com.elephantdrummer.exception.ParserException;



/**
 * Copyright 2018 Elephant Software Klaudiusz Wojtkowiak e-mail: [email protected]
 * 
 * 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. 
 */
public interface ParserDate  {

	
	public static Date toDateFromGreg(XMLGregorianCalendar from)  {
		if(from == null) return null;
        return from.toGregorianCalendar().getTime();

	}

	public static XMLGregorianCalendar toGreg(Date from) {
		if (from==null) return null;
		GregorianCalendar gCalendar = new GregorianCalendar();
        gCalendar.setTime(from);
        
        try {
        	XMLGregorianCalendar xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gCalendar);
            xmlCalendar.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
            return xmlCalendar;
        } catch (Exception ex) {
            throw new ParserException("Data parser from java.util.Date to javax.xml.datatype.XMLGregorianCalendar failed for value :"+from,ex);
        }
    }
	
	public static String parse(Date from,String pattern) {
		if (from==null) return null;
		return parse(from,new SimpleDateFormatThreadSafe(pattern));
	}
	
	public static String parse(Date from,SimpleDateFormatThreadSafe df) {
		if (from==null) return null;
		if (df==null) throw new ParserException("Invalid SimpleDateFormatThreadSafe");
//		try{
		String result;
		  synchronized(df) {
			 result= df.format(from);
		  }
		return result;
//		}catch (NullPointerException npe){
//			throw new ParserException("Nieprawidlowo zainicjowany SimpleDateFormatThreadSafe");
//		}
	}
	
//	public static Date parse(Long from) throws GSBException {
//		if (from==null) return null;
//		Date date = new Date(from);
//		return date;
//	}
	
	public static Long toLong(Date from) {
		if (from==null) return null;
		return from.getTime();
	}



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy