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

StringgetDDMMYYY_HHMM()
get DDMMYYHHMM
SimpleDateFormat DDMMYYYY_HHMM = new SimpleDateFormat("ddMMyyyy-HHmm");
return DDMMYYYY_HHMM.format(Calendar.getInstance().getTime());
StringgetDDMMYYYYDate(Date date)
get DDMMYYYY Date
return new SimpleDateFormat(DDMMYYYYhifenFormat).format(date);
StringgetFirstDate(String yearMonthStr, String yearMonthFormat, String dateFormat)
get First Date
DateFormat dfYearMonth = new SimpleDateFormat(yearMonthFormat);
DateFormat dfDate = new SimpleDateFormat(dateFormat);
java.util.Date date;
if (yearMonthStr == null || yearMonthStr.equals("")) {
    throw new Exception(yearMonthStr + " is invalid.");
try {
    date = dfYearMonth.parse(yearMonthStr);
...
DategetFormatToDateSimple(String yyyyMMdd)
get Format To Date Simple
java.util.Date date = new java.util.Date();
try {
    yyyyMMdd = yyyyMMdd.replaceAll("/", "").replaceAll(":", "");
    java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyyMMdd");
    date = formatter.parse(yyyyMMdd);
} catch (Exception e) {
    return null;
return date;
String[]getStartAndEndWeekOfMonth(int year, int month, int week, String format)
get Start And End Week Of Month
SimpleDateFormat formater = new SimpleDateFormat(format);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DATE, 0);
int lastWeek = calendar.get(Calendar.WEEK_OF_MONTH);
if (week > lastWeek)
    return null;
...
StringgetStringFormatMMYYWithoutHyphens(Date aDate)
get String Format MMYY Without Hyphens
SimpleDateFormat myFormatter = new SimpleDateFormat("MMyy");
if (aDate != null) {
    String myFormattedDate = myFormatter.format(aDate);
    return myFormattedDate;
return null;
StringgetStringFormatYYYYMMddWithHyphens(Date aDate)
get String Format YYYYM Mdd With Hyphens
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
if (aDate != null) {
    String myFormattedDate = myFormatter.format(aDate);
    return myFormattedDate;
return null;
StringgetTimeByYMD()
get Time By YMD
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = sdf.format(date);
String[] dateArray = dateStr.split("-");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dateArray.length; i++) {
    sb.append(dateArray[i]);
return sb.toString();
intgetYear(SimpleDateFormat df, String dateStr)
get Year
Calendar c = Calendar.getInstance();
try {
    c.setTime(df.parse(dateStr));
} catch (ParseException e) {
    e.printStackTrace();
return c.get(Calendar.YEAR);
StringgetYear(String date, String dateformat)
get Year
final SimpleDateFormat formatter = new SimpleDateFormat(dateformat);
final SimpleDateFormat year = new SimpleDateFormat("yyyy");
return year.format(formatter.parse(date));