Java Year Get getYear(String datetime)

Here you can find the source of getYear(String datetime)

Description

get Year

License

Open Source License

Declaration

public static int getYear(String datetime) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

import java.util.Calendar;
import java.util.Date;

public class Main {
    private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    public static int getYear(String datetime) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(parseDate(datetime));

        return cal.get(Calendar.YEAR);
    }/*from   w  ww.jav  a 2 s .  c  o m*/

    public static Date parseDate(String datetime) {
        try {
            return DATE_FORMAT.parse(datetime);
        } catch (ParseException e) {
            return new Date();
        }
    }

    public static Date parse(String datetime) {
        try {
            return FORMAT.parse(datetime);
        } catch (ParseException e) {
            return new Date();
        }
    }
}

Related

  1. getYear(String date)
  2. getYear(String date)
  3. getYear(String date)
  4. getYear(String date)
  5. getYear(String dateKey)
  6. getYear(String day)
  7. getYear(String index)
  8. getYear(String referralId)
  9. getYear(String startTime)