Java String to Date toDateFormat(Integer grain, TimeZone timeZone, String dateFormatArg)

Here you can find the source of toDateFormat(Integer grain, TimeZone timeZone, String dateFormatArg)

Description

to Date Format

License

Apache License

Declaration

public static DateFormat toDateFormat(Integer grain, TimeZone timeZone, String dateFormatArg) 

Method Source Code

//package com.java2s;
/*/*from  ww w .ja  v a 2s.c  om*/
 * Copyright 2012 ReachLocal 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.
 */

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    public static final SimpleDateFormat UTC_YEAR_FORMAT = new SimpleDateFormat("yyyy");
    public static final SimpleDateFormat UTC_MONTH_FORMAT = new SimpleDateFormat("yyyy-MM");
    public static final SimpleDateFormat UTC_DAY_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat UTC_HOUR_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH");

    public static DateFormat toDateFormat(Integer grain, TimeZone timeZone, String dateFormatArg) {
        if (dateFormatArg != null) {
            DateFormat result = new SimpleDateFormat(dateFormatArg);
            if (timeZone != null) {
                result.setTimeZone(timeZone);
            }
            return result;
        } else {
            return dateFormat(grain, timeZone);
        }
    }

    public static DateFormat toDateFormat(Integer grain, TimeZone timeZone, SimpleDateFormat dateFormatArg) {
        // we ignore the grain if there is a date format
        if (timeZone != null) {
            DateFormat result = new SimpleDateFormat(dateFormatArg.toPattern());
            result.setTimeZone(timeZone);
            return result;
        } else {
            return dateFormatArg;
        }
    }

    public static DateFormat dateFormat(int grain, TimeZone timeZone) {
        SimpleDateFormat result = dateFormat(grain);
        if (timeZone != null) {
            result = new SimpleDateFormat(result.toPattern());
            result.setTimeZone(timeZone);
        }
        return result;
    }

    public static SimpleDateFormat dateFormat(int grain) {
        switch (grain) {
        case Calendar.YEAR:
            return UTC_YEAR_FORMAT;
        case Calendar.MONTH:
            return UTC_MONTH_FORMAT;
        case Calendar.DAY_OF_MONTH:
            return UTC_DAY_FORMAT;
        default:
            return UTC_HOUR_FORMAT;
        }
    }
}

Related

  1. toDate(String time)
  2. toDate(String ts)
  3. toDate(String value)
  4. toDateByFormat(String dateString, String formatStr)
  5. toDateByPattern(String dateTime)
  6. toDateFormat(String date)
  7. toDateFormat(String dateFormat, TimeZone tz, Locale locale)
  8. toDateOracle(Date d, String format, String hqlFormat)
  9. toDates(DateFormat format, String[] values)