package skap.common.directOrder;
import java.util.*;
import java.sql.Date;
import java.io.*;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
/**
* dateの共通 BLBeanクラス。
*
*1.date共通BLBean
* thisis common class that get current time and date ,
* it can check date valide and format it to you want get date
*
* @authormiaogsh
* @version 1.0 2003/09/5 新規作成
*
*
* 更新履歴
* 更新日担当者 内容
*
*/
public class DateTime extends GregorianCalendar implements Serializable{
/**
* Serialized version unique id.
*/
public static final long serialVersionUID = -4669170878911604004L;
/**
* @serial
*/
public static TimeZone SPORE_TIMEZONE = TimeZone.getTimeZone("CTT"
;
/**
* Static variable defining date/time representation in DD/MM/YYYY HHmmss.0 format
*/
public static String SQL_OBJ_FORMAT = "YYYY-MM-DD HH:mm:ss.0";
/**
* Static variable defining date/time representation in DD/MM/YYYY HHmmss format
*/
public static String SQL_FORMAT = "YYYY-MM-DD HH:mm:ss";
/** Static variable defining date/time representation in YYYY-MM-DD format*/
public static String SQL_DATE = "YYYY-MM-DD";
/**
* Static variable defining date representation in DD/MM/YYYY format
*/
public static String STANDARD_DATE = "DD/MM/YYYY";
/**
* Static variable defining time representation in HHmmss format
*/
public static String STANDARD_TIME = "HH:mm:ss";
/**
* Static variable defining date/time representation in DD/MM/YYYY HH:mm:ss format
*/
public static String STANDARD_DATETIME = "DD/MM/YYYY HH:mm:ss";
static String[] mth_long = {"January","February","March","April","May","June",
"July","August","September","October","November","December"};
static String[] mth_short = {"JAN","FEB","MAR","APR","MAY","JUN",
"JUL","AUG","SEP","OCT","NOV","DEC"};
static char type = ' ';
static int count = 0;
static SimpleDateFormat df = (SimpleDateFormat)SimpleDateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.MEDIUM);
static DateTime datetime;
private boolean isNull = false;
/**
* Constructor that will set date/time to current date/time
*/
public DateTime()
{
super();
set(java.util.Calendar.MILLISECOND,0);
}
/**
* Constuctor taking in a date/time string in the specified format
*/
public DateTime(String dt_str, String format)
{
super();
dt_str = format(dt_str,format,"DDMMYYYYHHmmss"
;
if (dt_str!=null)
{
int day = (new Integer(dt_str.substring(0,2))).intValue();
int mth = (new Integer(dt_str.substring(2,4))).intValue() - 1;
int yr = (new Integer(dt_str.substring(4,8))).intValue();
int hr = (new Integer(dt_str.substring(8,10))).intValue();
int min = (new Integer(dt_str.substring(10,12))).intValue();
int sec = (new Integer(dt_str.substring(12,14))).intValue();
if (day == 0) day = 1;
set(yr,mth,day,hr,min,sec);
set(java.util.Calendar.MILLISECOND,0);
}else
{
isNull = true;
}
}
/**
* Constructor taking in 6 integers for YYYY, MM, DD, HH, mm and ss
*/
public DateTime(int YYYY, int MM, int DD, int HH, int mm, int ss){
super();
set(YYYY, MM-1, DD, HH, mm, ss);
set(java.util.Calendar.MILLISECOND,0);
}
/**
* Constructor taking in a long date
*/
public DateTime(long date){
super();
setTime(new Date(date));
}
/**
* Constructor taking in a java.sql.Date date
*/
public DateTime(java.sql.Date date)
{
super();
setTime(date);
}
/**
* Constructor taking in a locale
*/
public DateTime(Locale aLocale)
{
super(aLocale);
}
/**
* Constructor taking in a timezone
*/
public DateTime(TimeZone zone){
super(zone);
}
/**
* Constructor taking in a timezone and locale
*/
public DateTime(TimeZone zone, Locale aLocale){
super(zone, aLocale);
}
/**
* Date accessor returning date in DDMMYYYY format
*/
public String date(){
if (!isNull)
{
SimpleDateFormat df1 = new SimpleDateFormat("ddMMyyyy"
;
return df1.format(getTime());
}else
{
return null;
}
}
/**
* Time accessor returning date in HHmmss format
*/
public String time()
{
if (!isNull)
{
SimpleDateFormat df2 = new SimpleDateFormat("HHmmss"
;
return df2.format(getTime());
}else
{
return null;
}
}
/**
* Date initializer accepting date in DDMMYYYY format
*/
public void date(String d_str){
int day = (new Integer(d_str.substring(0,2))).intValue();
int mth = (new Integer(d_str.substring(2,4))).intValue() - 1;
int yr = (new Integer(d_str.substring(4,8))).intValue();
set(yr,mth,day);
set(java.util.Calendar.MILLISECOND,0);
}
/**
* Time initializer accepting date in HHmmss format
*/
public void time(String t_str){
int day = get(java.util.Calendar.DATE);
int mth = get(java.util.Calendar.MONTH);
int yr = get(java.util.Calendar.YEAR);
int hr = (new Integer(t_str.substring(0,2))).intValue();
int min = (new Integer(t_str.substring(2,4))).intValue();
int sec = (new Integer(t_str.substring(4,6))).intValue();
set(yr,mth,day,hr,min,sec);
set(java.util.Calendar.MILLISECOND,0);
}
/**
* Method to get date/time of the instance and return in the format required
*/
public String toString(String format){
if (!isNull)
return format(date()+time(),"DDMMYYYYHHmmss",format);
else
return null;
}
/**
* Over-riding method to return time in milliseconds
*/
public long getTimeInMillis(){
if (!isNull)
return super.getTimeInMillis();
else
return -1;
}
/**
* Over-riding method to set time in milliseconds
*/
public void setTimeInMillis(long millis){
super.setTimeInMillis(millis);
}
/**
* 230499 method to return time in seconds
*/
public int getTimeInSecs()
{
if (!isNull)
return ((int)(super.getTimeInMillis()/1000));
else
return -1;
}
/**
* Method to find if the year passed in is a leap year
*/
public static boolean isLeap(int y){
return (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) ? true : false;
}
/**
* Method to find the number of days in a particular month and year
*/
public static int getNoOfDaysInMth(int month, int year){
switch(month){
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return (isLeap(year) == true) ? 29:28;
default:
return 31;
}
}
/**
* Method to find the number of days in a particular year
*/
public static int getNoOfDaysInYr(int year){
return (isLeap(year) == true) ? 366:365;
}
/**
* Static method to get the current date/time in the user specified format
*/
public static String getDateTime(String format){
datetime = new DateTime();
return datetime.toString(format);
}
/**
* Static method to retrieve a date/time string representation of an SQL Date object in the specified format
*/
public static String getDateTime(Date date, String format){
return format(date.toString(),"YYYY-MM-DD",format);
}
/**
* Static method to get an SQL Date object passing in a date/time string and its format
*/
public static Date getSqlDate(String dt_str, String format){
return Date.valueOf(format(dt_str,format,"YYYY-MM-DD"
);
}
/**
* Static method to retrieve an SQL Date object containing the current date/time
*/
public static Date getSqlDate(){
datetime = new DateTime();
return Date.valueOf(datetime.toString("YYYY-MM-DD"
);
}
/**
* Static method to retrieve the string representation of the year of the datetime object
*/
public String year(){
if (!isNull)
return (toString("YYYY"
);
else
return null;
}
/**
* Static method to retrieve the string representation of the month of the datetime object
*/
public String month()
{
if (!isNull)
return (toString("MM"
);
else
return null;
}
/**
* Static method to retrieve the string representation of the day of month of the datetime object
*/
public String day(){
if (!isNull)
return (toString("DD"
);
else
return null;
}
/**
* Static method to retrieve the string representation of the current year
*/
//To be replaced by year()
public static String getYear(){
datetime = new DateTime();
return (datetime.toString("YYYY"
);
}
/**
* Static method to retrieve the string representation of the current month
*/
//To be replaced by month()
public static String getMonth(){
datetime = new DateTime();
return (datetime.toString("MM"));
}
/**
* Static method to retrieve the string representation of the current day of the month
*/
//To be replaced by day
public static String getDay(){
datetime = new DateTime();
return (datetime.toString("DD"));
}
/**
* Static method to retrieve the string representation of the current Minute
public static String getMinute() {
datetime = new DateTime();
return (datetime.toString("mm"));
}
*/
/**
* Static method to convert a date/time string from one format to another
*/
public static String format(String dt_str, String in_format, String out_format){
if (dt_str==null||dt_str.length()==0)
return null;
if (in_format==null||in_format.length()==0)
return null;
if (out_format==null||out_format.length()==0)return null;
int x = in_format.indexOf("MONTH");
while (x!=-1){
boolean success = false;
for (int i=0;i 0) {
d_arr[4] = '1';
d_arr[5] = '9';
}else{
d_arr[4] = '2';
d_arr[5] = '0';
}
//d_arr[4] = getYear().charAt(0);
//d_arr[5] = getYear().charAt(1);
}
}
x = in_format.indexOf("HH");
if (x!=-1){
if (!(Character.isDigit(dt_str.charAt(x)))||!(Character.isDigit(dt_str.charAt(x+1))))
return null;
int hour = Integer.parseInt(dt_str.substring(x,x+2));
if (hour23)
return null;
int y = in_format.indexOf("tt");
if (y!=-1){
if (dt_str.charAt(y+1)!='M'||dt_str.charAt(y+1)!='m')
return null;
if (dt_str.charAt(y)=='A'||dt_str.charAt(y)=='a'){
if (hour>11)
return null;
}else if (dt_str.charAt(y)=='P'||dt_str.charAt(y)=='p'){
if (hour12)
return null;
int y = in_format.indexOf("tt");
if (y!=-1){
if (dt_str.charAt(y+1)!='M'||dt_str.charAt(y+1)!='m')
return null;
if (dt_str.charAt(y)=='A'||dt_str.charAt(y)=='a'){
t_arr[0] = dt_str.charAt(x);
t_arr[1] = dt_str.charAt(x+1);
}else if (dt_str.charAt(y)=='P'||dt_str.charAt(y)=='p'){
hour = hour + 12;
String tmp = String.valueOf(hour);
t_arr[0] = tmp.charAt(0);
t_arr[1] = tmp.charAt(1);
}else
return null;
}
}
x = in_format.indexOf("mm");
if (x!=-1){
if (!(Character.isDigit(dt_str.charAt(x)))||!(Character.isDigit(dt_str.charAt(x+1))))
return null;
t_arr[2] = dt_str.charAt(x);
t_arr[3] = dt_str.charAt(x+1);
}
x = in_format.indexOf("ss");
if (x!=-1){
if (!(Character.isDigit(dt_str.charAt(x)))||!(Character.isDigit(dt_str.charAt(x+1))))
return null;
t_arr[4] = dt_str.charAt(x);
t_arr[5] = dt_str.charAt(x+1);
}
// Formatting date into required format
String D_str = String.valueOf(d_arr[0]) + String.valueOf(d_arr[1]);
String M_str = String.valueOf(d_arr[2]) + String.valueOf(d_arr[3]);
String Y_str = String.valueOf(d_arr[6]) + String.valueOf(d_arr[7]);
String YY_str = String.valueOf(d_arr[4]) + String.valueOf(d_arr[5]) + String.valueOf(d_arr[6]) + String.valueOf(d_arr[7]);
String h_str = String.valueOf(t_arr[0]) + String.valueOf(t_arr[1]);
String m_str = String.valueOf(t_arr[2]) + String.valueOf(t_arr[3]);
String s_str = String.valueOf(t_arr[4]) + String.valueOf(t_arr[5]);
x = out_format.indexOf("DD");
while (x!=-1){
out_format = out_format.substring(0,x) + D_str + out_format.substring(x+2);
x = out_format.indexOf("DD");
}
x = out_format.indexOf("MONTH");
while (x!=-1){
out_format = out_format.substring(0,x) + mth_long[Integer.parseInt(M_str)-1] + out_format.substring(x+5);
x = out_format.indexOf("MONTH");
}
x = out_format.indexOf("MON");
while (x!=-1){
out_format = out_format.substring(0,x) + mth_short[Integer.parseInt(M_str)-1] + out_format.substring(x+3);
x = out_format.indexOf("MON");
}
x = out_format.indexOf("MM");
while (x!=-1){
out_format = out_format.substring(0,x) + M_str + out_format.substring(x+2);
x = out_format.indexOf("MM");
}
x = out_format.indexOf("YYYY");
while (x!=-1){
out_format = out_format.substring(0,x) + YY_str + out_format.substring(x+4);
x = out_format.indexOf("YYYY");
}
x = out_format.indexOf("YY");
while (x!=-1){
out_format = out_format.substring(0,x) + Y_str + out_format.substring(x+2);
x = out_format.indexOf("YY");
}
x = out_format.indexOf("HH");
while (x!=-1){
out_format = out_format.substring(0,x) + h_str + out_format.substring(x+2);
x = out_format.indexOf("HH");
}
x = out_format.indexOf("hh");
while (x!=-1){
int tmp = Integer.parseInt(h_str);
if (tmp>12)
tmp = Integer.parseInt(h_str)-12;
else if (tmp==0)
tmp = 12;
if (tmp >= 10)
out_format = out_format.substring(0,x) + String.valueOf(tmp) + out_format.substring(x+2);
else
out_format = out_format.substring(0,x) + "0" + String.valueOf(tmp) + out_format.substring(x+2);
x = out_format.indexOf("hh");
}
x = out_format.indexOf("mm");
while (x!=-1){
out_format = out_format.substring(0,x) + m_str + out_format.substring(x+2);
x = out_format.indexOf("mm");
}
x = out_format.indexOf("ss");
while (x!=-1){
out_format = out_format.substring(0,x) + s_str + out_format.substring(x+2);
x = out_format.indexOf("ss");
}
x = out_format.indexOf("tt");
while (x!=-1){
String tmp;
if (Integer.parseInt(h_str)>11)
tmp = "PM";
else
tmp = "AM";
out_format = out_format.substring(0,x) + tmp + out_format.substring(x+2);
x = out_format.indexOf("tt");
}
return out_format;
}
/**
* Static method to find if a date in the specified format is valid
* if date is valide then return true else return false
* @date input date value
* @formatinput date formate
*/
public static boolean isValid(String date,String format){
if (date==null||date.length()==0)
return false;
date = format(date,format,"DDMMYYYYHHmmss");
if (date==null)
return false;
if (!(Character.isDigit(date.charAt(0))))
return false;
if (!(Character.isDigit(date.charAt(1))))
return false;
if (!(Character.isDigit(date.charAt(2))))
return false;
if (!(Character.isDigit(date.charAt(3))))
return false;
if (!(Character.isDigit(date.charAt(4))))
return false;
if (!(Character.isDigit(date.charAt(5))))
return false;
if (!(Character.isDigit(date.charAt(6))))
return false;
if (!(Character.isDigit(date.charAt(7))))
return false;
if (!(Character.isDigit(date.charAt(8))))
return false;
if (!(Character.isDigit(date.charAt(9))))
return false;
if (!(Character.isDigit(date.charAt(10))))
return false;
if (!(Character.isDigit(date.charAt(11))))
return false;
int _date = Integer.parseInt(date.substring(0,2));
int _month = Integer.parseInt(date.substring(2,4));
int _year = Integer.parseInt(date.substring(4,8));
if (_month>12)
return false;
if (_date>31)
return false;
if (_date>getNoOfDaysInMth(_month, _year) )
return false;
int _hour = Integer.parseInt(date.substring(8,10));
int _minute = Integer.parseInt(date.substring(10,12));
int _second = Integer.parseInt(date.substring(12,14));
if (_hour23)
return false;
if (_minute59)
return false;
if (_second59)
return false;
return true;
}
/**
* Static method to return a timestamp string in the format "YYYYMMDDhhmmss"
*/
public static String getTimeStamp(){
datetime = new DateTime();
return datetime.toString("YYYYMMDDHHmmss");
}
/**
* Temporary method to return an SQL date object given a date string in DD-MM-YYYY format
*/
public static Date getDate(String dt){
if (dt==null)
return getSqlDate();
else
return getSqlDate(dt,"DD-MM-YYYY");
}
/**
* Temporary method to return a date string in DD-MM-YYYY format given an SQL date object
*/
public static String stdFormat(Date dt){
return getDateTime(dt,STANDARD_DATE);
}
/**
* Temporary method to convert a date in YYYY-MM-DD format to DD-MM-YYYY format
*/
public static String stdFormat(String dt){
return format(dt,"YYYY-MM-DD",STANDARD_DATE);
}
public static int compare(DateTime date1, DateTime date2){
return date1.toString("YYYYMMDDHHmmss").compareTo(date2.toString("YYYYMMDDHHmmss"));
}
public static int compare(String date1, String format1, String date2, String format2){
date1 = format(date1,format1,"YYYYMMDDHHmmss");
date2 = format(date2,format2,"YYYYMMDDHHmmss");
return date1.compareTo(date2);
}
public int getDayNum(String startDt,String endDt) {
DateTime stDateTime = new DateTime(startDt, "YYYY/MM/DD");
DateTime edDateTime = new DateTime(endDt, "YYYY/MM/DD");
returngetDayNum(stDateTime, edDateTime);
}
public int getDayNum(DateTime startDt, DateTime endDt) {
int dayNum = -1;
java.util.Date stDate = startDt.getTime();
java.util.Date endDate= endDt.getTime();
long stSecNum = stDate .getTime();
long endSecNum= endDate.getTime();
long difNum = endSecNum - stSecNum;
double mSecOfDay= 24 * 60 * 60 * 1000;
double gDayNum
= difNum / mSecOfDay;
dayNum = (int)gDayNum;
return dayNum;
}
public static void main(String[] argv){
String format = "YYMMDD-HHmmss";
String dt1 = "990517-142930";
String dt2 = "990533-142930";
System.out.println(DateTime.isValid(dt1,format)+dt1);
System.out.println(DateTime.isValid(dt2,format)+dt2);
System.out.println(DateTime.format(dt1,format,"YYYY/MM/DD")+":"+dt1);
}
} |