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

org.xlcloud.console.utils.RawTimeConverter Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * 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 org.xlcloud.console.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

import javax.inject.Named;

/**
 * It converts raw time format stored as a Long value, to the format and time zone specified by the user.
 * @author "Konrad Król", AMG.net
 */
@Named
public class RawTimeConverter {
    
    private TimeZone timeZone = TimeZone.getTimeZone("UTC");
    
    private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    /**
     * @return value of time zone
     */
    public TimeZone getTimeZone() {
        return timeZone;
    }

    /**
     * Sets the value of time zone
     * @param timeZone time zone
     */
    public void setTimeZone(TimeZone timeZone) {
        this.timeZone = timeZone;
    }
    
    /**
     * @return date format
     */
    public DateFormat getDateFormat() {
        return dateFormat;
    }
    
    /**
     * Sets the requested date format
     * @param dateFormat date formamt
     */
    public void setDateFormat(DateFormat dateFormat) {
        this.dateFormat = dateFormat;
    }

    /**
     * Converts raw time to a string which contains date
     *  in the requested format,
     *  and for the requested time zone
     * @param time raw time
     * @return formatted date
     */
    public String formatTime(Long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(timeZone);
        cal.setTimeInMillis(time);
        
        return dateFormat.format(cal.getTime());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy