Java Year Format getYear(SimpleDateFormat df, String dateStr)

Here you can find the source of getYear(SimpleDateFormat df, String dateStr)

Description

get Year

License

Open Source License

Declaration

public static int getYear(SimpleDateFormat df, String dateStr) 

Method Source Code


//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {

    public static int getYear() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        return c.get(Calendar.YEAR);
    }/*from   ww  w.j  av  a  2 s  . c  o  m*/

    public static int getYear(SimpleDateFormat df, String dateStr) {
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(df.parse(dateStr));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return c.get(Calendar.YEAR);
    }

    public static Date parse(SimpleDateFormat df, String date) {
        try {
            return df.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }
}

Related

  1. getFormatToDateSimple(String yyyyMMdd)
  2. getStartAndEndWeekOfMonth(int year, int month, int week, String format)
  3. getStringFormatMMYYWithoutHyphens(Date aDate)
  4. getStringFormatYYYYMMddWithHyphens(Date aDate)
  5. getTimeByYMD()
  6. getYear(String date, String dateformat)
  7. getYear(String pFormattedDate)
  8. getYYMMDD()
  9. getYYMMDD(int i)