Berikut ini adalah pustaka tanggal yang bisa kubuat. Silahkan diambil jika berkenan dalam hati, dan silahkan dirubah untuk diperbaiki :)
/**
* Check wheater a string is match a date, string date format in Indonesian format (dd-mm-yyyy)
* or Indonesian datetime format (dd-mm-yyyy hh:mm:ss)
* @param strIdDate (String) Indonesian Date Format in dd-mm-yyyy or dd-mm-yyyy hh:mm:ss
* @return (Boolean) true if match false otherwise
*/
public static Boolean IsMatchDate(String strIdDate) {
int tgl, bln, thn, jam, mnt, dtk;
if (strIdDate.matches("^[0-3]{1}[0-9]{1}-[0-1]{1}[0-9]{1}-[1-2]{1}[0-9]{3}$")) {
tgl = Integer.parseInt(strIdDate.substring(0,2));
bln = Integer.parseInt(strIdDate.substring(3,5));
thn = Integer.parseInt(strIdDate.substring(6,10));
switch(bln) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return (tgl>0 && tgl<32);
case 4:
case 6:
case 9:
case 11:
return (tgl>0 && tgl<31);
case 2:
return ((thn%4)==0) ? (tgl>0 && tgl<30) : (tgl>0 && tgl<29);
default:
return false;
}
} else if (strIdDate.matches("^[0-3]{1}[0-9]{1}-[0-1]{1}[0-9]{1}-[1-2]{1}[0-9]{3} [0-2]{1}[0-9]{1}\\:[0-5]{1}[0-9]{1}\\:[0-5]{1}[0-9]{1}$")) {
tgl = Integer.parseInt(strIdDate.substring(0,2));
bln = Integer.parseInt(strIdDate.substring(3,5));
thn = Integer.parseInt(strIdDate.substring(6,10));
jam = Integer.parseInt(strIdDate.substring(11,13));
mnt = Integer.parseInt(strIdDate.substring(14,16));
dtk = Integer.parseInt(strIdDate.substring(17,19));
if ((jam>-1 && jam<24) && (mnt>-1 && mnt<60) && (dtk>-1 && dtk<60)) {
switch(bln) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return (tgl>0 && tgl<32);
case 4:
case 6:
case 9:
case 11:
return (tgl>0 && tgl<31);
case 2:
return ((thn%4)==0) ? (tgl>0 && tgl<30) : (tgl>0 && tgl<29);
default:
return false;
}
} else return false;
} else return false;
}
/**
* Fix string date format to Indonesian date format. The date format to convert is either ISO date or
* custom Indonesian date format.
* @param str (String) string date to fix
* @param isIsoDate (Boolean) true for yyyy-mm-dd.. otherwise false for dd-mm-yyyy..
* @return (String) Indonesian date format (dd-mm-yyyy) or date (dd-mm-yyyy hh:mm:ss), empty string on false
*/
public static String FixIDDateFormat(String str, Boolean isIsoDate) {
String tempDate = "";
if (isIsoDate) {
if (str.matches("^[1-2]{1}[0-9]{3}[0-2]{1}[0-9]{1}[0-3]{1}[0-9]{1}$")) {
// yyyymmdd
tempDate = str.substring(6, 8) + "-" + str.substring(4, 6) + "-" + str.substring(0, 4);
} else if (str.matches("^[1-2]{1}[0-9]{3}-[0-2]{1}[0-9]{1}-[0-3]{1}[0-9]{1}$")) {
// yyyy-mm-dd
tempDate = str.substring(8, 10) + "-" + str.substring(5, 7) + "-" + str.substring(0, 4);
} else if (str.matches("^[1-2]{1}[0-9]{3}-[0-2]{1}[0-9]{1}-[0-3]{1}[0-9]{1} [0-2]{1}[0-9]{1}\\:[0-5]{1}[0-9]{1}\\:[0-5]{1}[0-9]{1}$")) {
// yyyy-mm-dd hh:mm:ss
tempDate = str.substring(8, 10) + "-" + str.substring(5, 7) + "-" + str.substring(0, 4) + " " + str.substring(11, 19);
} else if (str.matches("^[1-2]{1}[0-9]{3}[0-2]{1}[0-9]{1}[0-3]{1}[0-9]{1}[0-2]{1}[0-9]{1}[0-5]{1}[0-9]{1}[0-5]{1}[0-9]{1}$")) {
// yyyymmddhhmmss
tempDate = str.substring(6, 8) + "-" + str.substring(4, 6) + "-" + str.substring(0, 4) + " " +
str.substring(8, 10) + ":" + str.substring(10, 12) + ":" + str.substring(12,14);
} else tempDate = "";
} else {
if (str.matches("^[0-3]{1}[0-9]{1}[0-1]{1}[0-9]{3}$")) {
// ddmmyy
int thn = Integer.parseInt(str.substring(4, 6));
thn = thn + ((thn < 27) ? 2000 : 1900);
tempDate = str.substring(0, 2) + "-" + str.substring(2, 4) + "-" + String.valueOf(thn).trim();
} else if (str.matches("^[0-3]{1}[0-9]{1}\\-[0-1]{1}[0-9]{1}\\-[0-9]{2}$")) {
// dd-mm-yy
int thn = Integer.parseInt(str.substring(6, 8));
thn = thn + ((thn < 27) ? 2000 : 1900);
tempDate = str.substring(0, 2) + "-" + str.substring(3, 5) + "-" + String.valueOf(thn).trim();
} else if (str.matches("^[0-3]{1}[0-9]{1}[0-1]{1}[0-9]{1}[1-2]{1}[0-9]{3}$")) {
// ddmmyyyy
tempDate = str.substring(0, 2) + "-" + str.substring(2, 4) + "-" + str.substring(4, 8);
} else if (str.matches("^[0-3]{1}[0-9]{1}[0-1]{1}[0-9]{1}[1-2]{1}[0-9]{3}[0-2]{1}[0-9]{1}[0-5]{1}[0-9]{1}[0-5]{1}[0-9]{1}$")) {
// ddmmyyyyhhmmss
tempDate = str.substring(0, 2) + "-" + str.substring(2, 4) + "-" + str.substring(4, 8) + " " +
str.substring(8, 10) + ":" + str.substring(10, 12) + ":" + str.substring(12, 14);
} else if (str.matches("^[0-3]{1}[0-9]{1}\\/[0-1]{1}[0-9]{1}\\/[0-9]{2}$")) {
// dd/mm/yy
int thn = Integer.parseInt(str.substring(6, 8));
thn = thn + ((thn < 27) ? 2000 : 1900);
tempDate = str.substring(0, 2) + "-" + str.substring(3, 5) + "-" + String.valueOf(thn).trim();
} else if (str.matches("^[0-3]{1}[0-9]{1}\\/[0-1]{1}[0-9]{1}\\/[1-2]{1}[0-9]{3}$")) {
// dd/mm/yyyy
tempDate = str.replaceAll("\\/", "-");
} else if (str.matches("^[0-3]{1}[0-9]{1}\\/[0-1]{1}[0-9]{1}\\/[1-2]{1}[0-9]{3} [0-2]{1}[0-9]{1}\\:[0-5]{1}[0-9]{1}\\:[0-5]{1}[0-9]{1}$")) {
// dd/mm/yyyy hh:mm:ss
tempDate = str.replaceAll("\\/", "-");
} else tempDate = str;
}
return (IsMatchDate(tempDate)) ? tempDate : "";
}
/**
* Convert Indonesian date format to Foxpro DTOS style date :)
* @param strIDDate (String) Indonesian dd-mm-yyyy date to convert
* @return (String) Foxpro's DTOS date style format yyyymmdd
*/
public static String DTOSDate(String strIDDate) {
return strIDDate.substring(6, 10) + strIDDate.substring(3, 5) + strIDDate.substring(0, 2);
}
/**
* Get current system date
* @return (java.sql.Date) current system date
*/
public static Date GetCurrentSystemDate() {
return new Date(new java.util.Date().getTime());
}
/**
* Convert String date to java.sql.Date. If toDate has time hours, minutes and seconds then
* it will used otherwise it will truncated to date only
* @param toDate (String) string date to convert
* @param isoDate (Boolean) true for isoDate string otherwise false for Indonesian date String format
* @return (java.sql.Date) converted string date or null if no match in form
* @throws Exception (Exception) when thereis an error occured
*/
public static Date StringToDate(String toDate, Boolean isoDate) {
Date result = null;
try {
if ((toDate = FixIDDateFormat(toDate, isoDate))!="") {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
if (toDate.length()==10) sdf = new SimpleDateFormat("dd-MM-yyyy");
result = new Date(sdf.parse(toDate).getTime());
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
return result;
}
/**
* Convert java.sql.date to Indonesian form String date format (dd-mm-yyyy or dd-mm-yyyy hh:mm:ss)
* @param toIDDate (java.sql.Date) date to convert
* @param withTime (Boolean) get time instead date
* @return Indonesian formatted string date ir datetime
*/
public static String DateToStringIDDate(Date toIDDate, Boolean withTime) {
if (toIDDate == null) return "";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
if (!withTime) sdf = new SimpleDateFormat("dd-MM-yyyy");
return sdf.format(new java.util.Date(toIDDate.getTime()));
}
/**
* Get age of year between pastDate and futureDate
* @param pastDate (java.sql.Date) past date
* @param futureDate (java.sql.Date) future date
* @return (int) the ages of year
*/
public static int GetAge(Date pastDate, Date futureDate) {
int age = 0;
if (pastDate == null || futureDate == null) return age;
Calendar cp = Calendar.getInstance(), cf = Calendar.getInstance();
cp.setTime(pastDate);
cf.setTime(futureDate);
age = cf.get(Calendar.YEAR) - cp.get(Calendar.YEAR);
if (cf.get(Calendar.MONTH) <= cp.get(Calendar.MONTH)) {
if (cf.get(Calendar.MONTH) < cp.get(Calendar.MONTH)) age--;
if (cf.get(Calendar.MONTH) == cp.get(Calendar.MONTH) &&
cf.get(Calendar.DAY_OF_MONTH) < cp.get(Calendar.DAY_OF_MONTH)) age--;
}
return age;
}
/**
* Convert java.sql.Date to ddmmyy formatted date
* @param toCompact (java.sql.Date)
* @return (String) ddmmyy of toCompact
*/
public static String CompactDate(Date toCompact) {
if (toCompact!=null) {
String strDate = DateToStringIDDate(toCompact, Boolean.FALSE);
return strDate.substring(0, 2) + strDate.substring(3, 5) + strDate.substring(8, 10);
} else return "000000";
}
Langganan:
Posting Komentar (Atom)
nice post man...
BalasHapus