Java Calendar Format getDate(Calendar cal, String dateFormat)

Here you can find the source of getDate(Calendar cal, String dateFormat)

Description

get Date

License

Open Source License

Declaration

public static String getDate(Calendar cal, String dateFormat) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) Emil Crumhorn - Hexapixel.com - emil.crumhorn@gmail.com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from w  w  w  . ja  va 2  s  .  c  om
 *    emil.crumhorn@gmail.com - initial API and implementation
 *******************************************************************************/

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

import java.util.Locale;
import java.util.Map;

public class Main {
    private static Map _dateFormatMap;
    private static Locale _locale;

    public static String getDate(Calendar cal, String dateFormat) {
        //Calendar toUse = (Calendar) cal.clone();
        Calendar toUse = Calendar.getInstance(_locale);
        toUse.setTime(cal.getTime());
        toUse.add(Calendar.MONTH, -1);

        /*      HashMap dMap = null;
          if (fastDateMap.get(cal) != null) {
         dMap = (HashMap) fastDateMap.get(cal);
         if (dMap.get(dateFormat) != null) {
            System.err.println("Returned old");
            return (String) dMap.get(dateFormat);
         }
          }
        */
        SimpleDateFormat df = null;
        if (_dateFormatMap.get(dateFormat) != null) {
            df = (SimpleDateFormat) _dateFormatMap.get(dateFormat);
        } else {
            df = new SimpleDateFormat(dateFormat, _locale);
            _dateFormatMap.put(dateFormat, df);
        }

        df.setLenient(true);
        //String ret = df.format(cal.getTime());

        /*      // cache it
          if (dMap == null) 
         dMap = new HashMap();
              
          System.err.println("Created new " + cal.getTime());
              
          dMap.put(dateFormat, ret);
          fastDateMap.put(cal, dMap);
        */
        return df.format(cal.getTime());
    }
}

Related

  1. formatDay(java.util.Calendar d)
  2. formatDuration(Calendar t1, Calendar t2, boolean precise)
  3. formatRfc3339Calendar(Calendar cal)
  4. formatTime(Calendar cal, StringBuffer sb)
  5. formatToHTTPDate(Calendar calendar)
  6. getDate(Calendar cal, String dateFormat)
  7. print(Calendar[] calendar)
  8. printCalendar(Calendar cal)
  9. toInfo(Calendar calendar)