Java Year Get getYear(String strDate)

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

Description

get Year

License

Open Source License

Declaration

public static final int getYear(String strDate) 

Method Source Code


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

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    private static String defaultDatePattern = "yyyy-MM-dd";
    public static SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static final int getYear(String strDate) {

        Calendar cale = toCalendar(strDate);
        if (cale == null) {
            return -1;
        }/*from  w  ww  .j  av  a 2  s  .  c o m*/
        return cale.get(Calendar.YEAR);
    }

    private static final Calendar toCalendar(String strDate) {
        Calendar cale = null;
        try {
            Date thisDate = dateTimeFormatter.parse(strDate);
            cale = Calendar.getInstance();
            cale.setTime(thisDate);
        } catch (Exception e) {
            return null;
        }
        return cale;
    }

    public static Date parse(String strDate) throws ParseException {
        return parse(strDate, getDatePattern());
    }

    public static Date parse(String strDate, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        try {
            return df.parse(strDate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static Date parse(Date strDate, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        try {
            return df.parse(df.format(strDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getDatePattern() {
        return defaultDatePattern;
    }

    public static String format(Date date) {
        return format(date, getDatePattern());
    }

    public static String format(Date date, String pattern) {
        String returnValue = "";

        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            returnValue = df.format(date);
        }

        return (returnValue);
    }
}

Related

  1. getYear(String day)
  2. getYear(String index)
  3. getYear(String referralId)
  4. getYear(String startTime)
  5. getYear(String str_date)
  6. getYear(String strDate)
  7. getYear(String string)
  8. getYear(String tempdat, Locale locale)
  9. getYear(String[] contents)