Java Date Format dateFormat(int grain, TimeZone timeZone)

Here you can find the source of dateFormat(int grain, TimeZone timeZone)

Description

date Format

License

Apache License

Declaration

public static DateFormat dateFormat(int grain, TimeZone timeZone) 

Method Source Code

//package com.java2s;
/*//from w w w  . j a  v a 2  s. 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 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. dateFormat(Date date, String format)
  2. dateFormat(Date date, String pattern)
  3. dateFormat(Date date, String pattern)
  4. dateFormat(Date date, String pattern)
  5. dateFormat(DateFormat f, Date d)
  6. dateFormat(java.util.Date date, String formatStr)
  7. dateFormat(long time)
  8. dateFormat(String date, String dateFormat)
  9. DateFormat(String dateStr, String oldPattern, String newPattern)