Java Month Name Get getMonthName(String dateString)

Here you can find the source of getMonthName(String dateString)

Description

get Month Name

License

Open Source License

Declaration

public static String getMonthName(String dateString) 

Method Source Code

//package com.java2s;
/*//from   w ww . ja  va  2  s.  co  m
 * Copyright (c) 2013 The Johns Hopkins University/Applied Physics Laboratory
 *                             All rights reserved.
 *
 * This material may be used, modified, or reproduced by or for the U.S.
 * Government pursuant to the rights granted under the clauses at
 * DFARS 252.227-7013/7014 or FAR 52.227-14.
 *
 * 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
 *
 * NO WARRANTY.   THIS MATERIAL IS PROVIDED "AS IS."  JHU/APL DISCLAIMS ALL
 * WARRANTIES IN THE MATERIAL, WHETHER EXPRESS OR IMPLIED, INCLUDING (BUT NOT
 * LIMITED TO) ANY AND ALL IMPLIED WARRANTIES OF PERFORMANCE,
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT OF
 * INTELLECTUAL PROPERTY RIGHTS. ANY USER OF THE MATERIAL ASSUMES THE ENTIRE
 * RISK AND LIABILITY FOR USING THE MATERIAL.  IN NO EVENT SHALL JHU/APL BE
 * LIABLE TO ANY USER OF THE MATERIAL FOR ANY ACTUAL, INDIRECT,
 * CONSEQUENTIAL, SPECIAL OR OTHER DAMAGES ARISING FROM THE USE OF, OR
 * INABILITY TO USE, THE MATERIAL, INCLUDING, BUT NOT LIMITED TO, ANY DAMAGES
 * FOR LOST PROFITS.
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class Main {
    public static String getMonthName(String dateString) {
        String returnString;
        java.util.Date date = getDate(dateString);
        SimpleDateFormat sdf = new SimpleDateFormat("MMM");
        returnString = sdf.format(date);
        return returnString;
    }

    public static java.util.Date getDate(String dateString) {
        java.util.Date returnDate = null;
        SimpleDateFormat sdf = getStandardDateFormat();
        if (dateString != null) {
            try {
                returnDate = sdf.parse(dateString);
            } catch (ParseException pe) {
                System.err.println("ERROR - [DateHelper.getDate(String)] - " + pe.getMessage());
                pe.printStackTrace();
            }
        }
        return returnDate;
    }

    /**
     * Returns Date object for specified number of days ago, relative to today. Allow the ability to pass in what the
     * current date is.
     *
     * @return Date
     */
    public static java.util.Date getDate(int numDaysAgo) {
        //BioSrvConfig config = BioSrvConfig.getInstance();
        String currentDateArg = null;
        java.util.Date returnDate = null;

        Calendar cal = new GregorianCalendar();

        //check currentDate Arge in biosrvconfig
        //currentDateArg = config.getCurrentDateArg();
        if (currentDateArg == null
        //|| currentDateArg.equals(BioSrvConfig.today_currentDateArgValue)) {
        ) {
            cal.setTime(new java.util.Date());
        } else {
            cal.setTime(getDate(currentDateArg));
            //      System.err.println("Today is " + getDate(currentDateArg));
        }

        cal.add(Calendar.DATE, 0 - numDaysAgo);
        returnDate = cal.getTime();

        return returnDate;
    }

    /**
     * Returns Date object for specified number of days ago, relative to specified Date object.
     */
    public static java.util.Date getDate(java.util.Date fromDate, int numDaysAgo) {
        java.util.Date returnDate;

        Calendar cal = new GregorianCalendar();
        cal.setTime(fromDate);
        cal.add(Calendar.DATE, 0 - numDaysAgo);
        returnDate = cal.getTime();

        return returnDate;
    }

    /**
     * Returns String representing date a specified number of days ago, relative to supplied date String.
     */
    public static String getDate(String fromDateString, int numDaysAgo) {
        String returnDateString;

        java.util.Date fromDate = getDate(fromDateString);
        Calendar cal = new GregorianCalendar();
        cal.setTime(fromDate);
        cal.add(Calendar.DATE, 0 - numDaysAgo);
        java.util.Date returnDate = cal.getTime();
        returnDateString = getFormattedDateString(returnDate);

        return returnDateString;
    }

    public static java.util.Date getDate(String day, String month, String year) {
        java.util.Date returnDate = null;
        Calendar calendar = new GregorianCalendar();
        try {
            calendar.set(Calendar.YEAR, Integer.parseInt(year));
            calendar.set(Calendar.MONTH, Integer.parseInt(month));
            calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
            returnDate = calendar.getTime();
        } catch (Exception e) {
            System.out.println("DateHelper.getDate(int, int, int): Illegal date");
        }
        return returnDate;
    }

    public static SimpleDateFormat getStandardDateFormat() {
        // cas 07-13-03
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //SimpleDateFormat sdf = new SimpleDateFormat("MMddyy");
        SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyy");
        return sdf;
    }

    public static String getFormattedDateString(java.util.Date date) {
        String returnString = "";

        SimpleDateFormat sdf = getStandardDateFormat();
        returnString = sdf.format(date);

        return returnString;
    }

    public static String getFormattedDateString(int numDaysAgo) {
        String returnString = "";

        java.util.Date returnDate = getDate(numDaysAgo);
        SimpleDateFormat sdf = getStandardDateFormat();
        returnString = sdf.format(returnDate);

        return returnString;
    }
}

Related

  1. getMonthName(int index)
  2. getMonthName(int month, Locale locale)
  3. getMonthName(int MonthNumber)
  4. getMonthName(int monthNumber)
  5. getMonthName(String code)
  6. getMonthName(String month)
  7. getMonthNames(Locale locale)
  8. getMonthNames(Locale locale)
  9. getMonthSelect(String selectName, String value, boolean hasBlank)