Java Utililty Methods Year Format

List of utility methods to do Year Format

Description

The list of methods to do Year Format are organized into topic(s).

Method

booleanisPartialYear(String s, String yearFormat)
Allow only 4 digits, no more, no less
int dn = 0;
char[] cyear = s.toCharArray();
for (char c : cyear) {
    if (!Character.isDigit(c)) {
        return false;
    ++dn;
if (dn != 4) {
    return false;
String yearformat = parseDateFormat(yearFormat) + "-MM-dd";
SimpleDateFormat sdf_y = new SimpleDateFormat(yearformat);
sdf_y.setLenient(false);
String sy = s + "-01-18";
try {
    sdf_y.parse(sy);
    return true;
} catch (Exception e) {
    return false;
StringisValidDateMMddyyyy(String theString)
is Valid Date M Mddyyyy
String dateFormat = MMddyyyyFORMATSlashes;
String dateRegexp = MMddyyyySlashes;
return ifValidDateFormatAsyyyyMMdd(theString, dateFormat, dateRegexp);
booleanisYear(int y)
is Year
return y >= 100 && y <= 5000;
booleanisYear(String str, boolean nullCheck)
is Year
if (nullCheck == false || isRequired(str)) {
    if (str == null) {
        return true;
    if (isNumber(str, false)) {
        int year = Integer.parseInt(str.trim());
        if (year < 1500 && year > 2999) {
            return false;
...
booleanisYearOnly(String input)
is Year Only
return input.matches("[0-9]{4}");
booleanisYYYY(String strDate)
is YYYY
try {
    parse(strDate, YYYY);
    return true;
} catch (ParseException pe) {
    return false;
StringlastDayOfWeek(String year, int week, String format)
last Day Of Week
SimpleDateFormat dayFormat = new SimpleDateFormat(format, Locale.UK);
Calendar calendar = Calendar.getInstance(Locale.UK);
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setMinimalDaysInFirstWeek(4);
calendar.set(Calendar.WEEK_OF_YEAR, week);
calendar.set(Calendar.YEAR, Integer.parseInt(year));
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
Date date = calendar.getTime();
...
DatestringYYYYmmDDhhMMssToDate(String value)
string YYY Ymm D Dhh M Mss To Date
try {
    SimpleDateFormat dbUpdateDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return dbUpdateDateTime.parse(value);
} catch (ParseException ex) {
return null;
StringtoDateString(String ddMMMyyyy)
to Date String
String dateString = "";
try {
    Date date = new SimpleDateFormat("ddMMMyyyy", Locale.US).parse(ddMMMyyyy);
    dateString = new SimpleDateFormat("yyyy-MM-dd", Locale.US).format(date);
} catch (Exception e) {
    e.printStackTrace();
    return "";
return dateString;
StringtoYYYYMM(Date date)
to YYYYMM
return toYYYYMM(date, null);