everyDateinterpoint (interword separation)timestring.java.util.Date, ,Calendarconversion to
Java can be used to create all kinds ofDate, time stringif possible. Calendar , , java.util.Date The following is a sample program that converts to the
Conversion requires formatting to the default date/time string and then Calendar type. If conversion is not possible, an IllegalArgumentException will be thrown.
Also,java.util.Date The getTime() method of the Calendar class is used for conversion to the
The date formats that can be converted are as follows
sample program
int yyyy = Integer.parseInt(strDate.substring(0,4));
int MM = Integer.parseInt(strDate.substring(5,7));
int dd = Integer.parseInt(strDate.substring(8,10));
int HH = cal.get(Calendar.HOUR_OF_DAY);
int mm = cal.get(Calendar.MINUTE);
int ss = cal.get(Calendar.SECOND);
int SSS = cal.get(Calendar.MILLISECOND);
cal.clear();
cal.set(yyyy,MM-1,dd);
int len = strDate.length();
switch (len) {
case 10:
break;
case 16: // yyyy/MM/dd HH:mm
HH = Integer.parseInt(strDate.substring(11,13));
mm = Integer.parseInt(strDate.substring(14,16));
cal.set(Calendar.HOUR_OF_DAY,HH);
cal.set(Calendar.MINUTE,mm);
break;
case 19: //yyyy/MM/dd HH:mm:ss
HH = Integer.parseInt(strDate.substring(11,13));
mm = Integer.parseInt(strDate.substring(14,16));
ss = Integer.parseInt(strDate.substring(17,19));
cal.set(Calendar.HOUR_OF_DAY,HH);
cal.set(Calendar.MINUTE,mm);
cal.set(Calendar.SECOND,ss);
break;
case 23: //yyyy/MM/dd HH:mm:ss.SSS
HH = Integer.parseInt(strDate.substring(11,13));
mm = Integer.parseInt(strDate.substring(14,16));
ss = Integer.parseInt(strDate.substring(17,19));
SSS = Integer.parseInt(strDate.substring(20,23));
cal.set(Calendar.HOUR_OF_DAY,HH);
cal.set(Calendar.MINUTE,mm);
cal.set(Calendar.SECOND,ss);
cal.set(Calendar.MILLISECOND,SSS);
break;
default :
throw new IllegalArgumentException(
“Argument string[“+ strDate +
“] cannot be converted to a date string”);
}
return cal;
}
/**
* Default date/time format for various date/time strings
Convert to *.
*
* ●The default date format is as follows.
* For date only: yyyy/MM/dd
* For date + time: yyyy/MM/dd HH:mm:ss.SSS
*
* @param str String to convert
* @return Default date/time format
* @throws IllegalArgumentException
* If the date string cannot be converted
*/
private static String format(String str){
if (str == null || str.trim().length() < 8) {
throw new IllegalArgumentException(
“Argument string [“+ str +
“] cannot be converted to a date string”);
}
str = str.trim();
String yyyy = null; String MM = null; String dd = null;
String HH = null; String mm = null;
String ss = null;String ss = null;
// If “-” or “/” is missing
if (str.indexOf(“/”)==-1 && str.indexOf(“-“)==-1) {
if (str.length() == 8) {
yyyy = str.substring(0,4);
MM = str.substring(4,6);
dd = str.substring(6,8);
return yyyy+”/”+MM+”/”+dd;
}
yyyy = str.substring(0,4);
MM = str.substring(4,6);
dd = str.substring(6,8);
HH = str.substring(9,11);
mm = str.substring(12,14);
ss = str.substring(15,17);
return yyyy+”/”+MM+”/”+dd+” “+HH+”:”+mm+”:”+ss;
}
StringTokenizer token = new StringTokenizer(str,”_/-:. “);
StringBuffer result = new StringBuffer();
for(int i = 0; token.hasMoreTokens(); i++) {
String temp = token.nextToken();
switch(i){
case 0:// year part
yyyy = fillString(str, temp, “L”, “20”, 4);
result.append(yyyy);
break;
case 1:// month part
MM = fillString(str, temp, “L”, “0”, 2);
result.append(“/”+MM);
break;
case 2:// day part
dd = fillString(str, temp, “L”, “0”, 2);
result.append(“/”+dd);
break;
case 3:// time part
HH = fillString(str, temp, “L”, “0”, 2);
result.append(” “+HH);
break;
case 4:// minute part
mm = fillString(str, temp, “L”, “0”, 2);
result.append(“:”+mm);
break;
case 5:// second part
ss = fillString(str, temp, “L”, “0”, 2);
result.append(“:”+ss);
break;
case 6:// millisecond part
SSS = fillString(str, temp, “R”, “0”, 3);
result.append(“.”+SSS);
break;
}
}
return result.toString();
}
private static String fillString(String strDate, String str,
String position, String addStr,
int len){
if (str.length() > len) {
throw new IllegalArgumentException(
“Argument string[“+ strDate +
“] cannot be converted to a date string”);
}
return fillString(str, position, len,addStr);
}
/**
* Add the string [addStr] to be added to the string [str]
* Insert at [position] until [len] is filled.
*
* *Even if [str] is null or an empty literal, use [addStr]
* Returns the result inserted until [len] is filled.
* @param str target string
* @param position Insert before ⇒ L or l Insert after ⇒ R or r
* @param len Number of digits to replenish
* @param addStr String to insert
* @return The string after conversion.
*/
private static String fillString(String str, String position,
int len,
String addStr) {
if (addStr == null || addStr.length() == 0) {
throw new IllegalArgumentException
(“The value of the string to be inserted is invalid. addStr=”+addStr);
}
if (str == null) {
str = “”;
}
StringBuffer buffer = new StringBuffer(str);
while (len > buffer.length()) {
if (position.equalsIgnoreCase(“l”)) {
int sum = buffer.length() + addStr.length();
if (sum > len) {
addStr = addStr.substring
(0,addStr.length() – (sum – len));
buffer.insert(0, addStr);
}else{
buffer.insert(0, addStr);
}
} else {
buffer.append(addStr);
}
}
if (buffer.length() == len) {
return buffer.toString();
}
return buffer.toString().substring(0, len);
}
Execution Result
◆Example of Execution
// yyyy-MM-dd pattern
cal = toCalendar(“2007-01-01”);
date = cal.getTime();
System.out.println(“[2007-01-01] = '”+date+”’”);
//yyyyMMdd pattern
cal = toCalendar(“20070101”);
date = cal.getTime();
System.out.println(“[20070101] = '”+date+”’”);
// yy-MM-dd pattern
cal = toCalendar(“07-01-01”);
date = cal.getTime();
System.out.println(“[07-01-01] = '”+date+”’”);
System.out.println(“\n—– Date+Time ————————–“);
// pattern of yyyy/MM/dd HH:mm:ss
cal = toCalendar(“2007/01/01 12:00:00”);
date = cal.getTime();
System.out.println(“[2007/01/01 12:00:00] = '”+date+”’”);
// pattern of yyyyMMdd HH:mm:ss
cal = toCalendar(“20070101 12:00:00”);
date = cal.getTime();
System.out.println(“[20070101 12:00:00] = '”+date+”’”);
// pattern of yyyy/MM/dd HH:mm
cal = toCalendar(“2007/01/01 12:00”);
date = cal.getTime();
System.out.println(“[2007/01/01 12:00] = '”+date+”’”);
System.out.println(“\n—– Time only —————————“);
// Pattern of HH:mm:ss. *If only the time is used, add the date in front.
cal = toCalendar(
new SimpleDateFormat(“yyyy/MM/dd”).format(new Date()) +
” 12:00:00″);
date = cal.getTime();
System.out.println(“[12:00:00] = '”+date+”’”);
}
◆Output result
----- Date only --------------------------- [2007/01/01] = 'Mon Jan 01 00:00:00 JST 2007' [2007-01-01] = 'Mon Jan 01 00:00:00 JST 2007' [20070101] = 'Mon Jan 01 00:00:00 JST 2007' [07-01-01] = 'Mon Jan 01 00:00:00 JST 2007' ----- Date + Time -------------------------- [2007/01/01 12:00:00] = 'Mon Jan 01 12:00:00 JST 2007' [2007/01/01 12:00:00] = 'Mon Jan 01 12:00:00 JST 2007' [2007/01/01 12:00] = 'Mon Jan 01 12:00:00 JST 2007' ----- time only --------------------------- [12:00:00] = 'Wed Jun 06 12:00:00 JST 2007'
This sample program is a Chat&Messenger "Calendar and scheduling functionsThe actual use of the system is done in the "I'm a fan of this system.