Android Long to Year Convert parseYearFromDate(String longDate)

Here you can find the source of parseYearFromDate(String longDate)

Description

Return the release year from a date on the format "YYYY-MM-DD".

License

MIT License

Parameter

Parameter Description
longDate The formatted long date

Return

The year as a string on the format "YYYY". If the parameter is not well formated ("YYYY-MM-DD") the parameter is returned untouched.

Declaration

public static String parseYearFromDate(String longDate) 

Method Source Code

//package com.java2s;
/**//from   w  w  w . ja va  2 s .c o  m
 *   DateTimeUtils.java
 *
 *  A utility class for converting dates to common formats
 *
 *   @author Robin Andersson, Johan Brook
 *   @copyright (c) 2012 Robin Andersson, Johan Brook, Mattias Henriksson, Lisa Stenberg
 *   @license MIT
 */

public class Main {
    /**
     * Return the release year from a date on the format "YYYY-MM-DD".
     * 
     * @param longDate
     *            The formatted long date
     * @return The year as a string on the format "YYYY". If the parameter is
     *         not well formated ("YYYY-MM-DD") the parameter is returned
     *         untouched.
     */
    public static String parseYearFromDate(String longDate) {
        if (longDate == null)
            return null;

        int index = longDate.indexOf("-");
        return (index != -1) ? longDate.substring(0, index) : longDate;
    }
}

Related

  1. getYear(long dateTimeMillis)