DateTimeUtil.java Source code

Java tutorial

Introduction

Here is the source code for DateTimeUtil.java

Source

/**
 * 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.utils;

import java.sql.*;
import java.util.*;
import java.text.*;

/**
 *
 * @author Marco Berri marcoberri@gmail.com
 */
public final class DateTimeUtil {

    /**
     *
     */
    public static final String tsDefaultFormats[] = { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "HH:mm:ss", "HH:mm",
            "yyyy-MM" };

    /**
     * Return a String value of Now() in a specify format
     * @param lang lingua di default
     * @param format formato della data vedi esempi tsDefaultFormats
     * @return String value of Now() in a specify format
     */
    public static final String getNowWithFormat(String format, String lang) {
        Calendar cal = Calendar.getInstance(TimeZone.getDefault());
        String DATE_FORMAT = format;
        SimpleDateFormat sdf = null;

        if (lang != null) {
            sdf = new SimpleDateFormat(DATE_FORMAT, new Locale(lang));
        } else {
            sdf = new SimpleDateFormat(DATE_FORMAT);
        }

        sdf.setTimeZone(TimeZone.getDefault());
        return sdf.format(cal.getTime());
    }//getNowWithFormat()
}