Java Year Get getYearName(String dateString)

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

Description

get Year Name

License

Open Source License

Declaration

public static String getYearName(String dateString) 

Method Source Code

//package com.java2s;
/*//  w w w . j  ava  2  s.c o 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 getYearName(String dateString) {
        String returnString;
        java.util.Date date = getDate(dateString);
        SimpleDateFormat sdf = new SimpleDateFormat("yy");
        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. getYearEnd(Date date)
  2. getYearForDate(String dateToCheck, String pattern)
  3. getYearList(int years)
  4. getYearLongDesc(Date year)
  5. getYearlyIntervals(Date start, Date end)
  6. getYearsOfAge(Date birthDate, Date nowDate)
  7. getYearString()
  8. getYearWeek(int year, int week)