📄 convertdate.java
字号:
/***编写作者: comingnet ********/
/***编写时间:20070201 ********/
/***修改作者:gemhoo ********/
/***修改时间:20070207 ********/
/***修改说明: ijipin->comingnet ********/
/*** 类名: ConvertDate.java ********/
/***功能说明: 工具配置类 ********/
/*** ********/
package com.comingnet.tool;
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ConvertDate
{
public ConvertDate()
{
}
public Date stringToDate(String str)
{
String strFormat = "yyyy-MM-dd HH:mm:ss";
int iLen = str.length();
if(str == null)
return null;
iLen = str.length();
if(iLen == 10)
strFormat = "yyyy-MM-dd";
else
if(iLen == 16)
strFormat = "yyyy-MM-dd HH:mm";
else
if(iLen == 19)
strFormat = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdFormat = new SimpleDateFormat(strFormat);
Date date = new Date();
try
{
date = sdFormat.parse(str);
}
catch(Exception e)
{
Date date1 = null;
return date1;
}
return date;
}
public Date stringToDate(String str, String strFormat)
{
SimpleDateFormat sdFormat = new SimpleDateFormat(strFormat);
Date date = new Date();
try
{
date = sdFormat.parse(str);
}
catch(Exception e)
{
Date date1 = null;
return date1;
}
return date;
}
public String dateToYMD(Date dt)
{
SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
String str = "";
try
{
str = sdFormat.format(dt);
}
catch(Exception e)
{
String s = "";
return s;
}
if(str.equals("1900-01-01"))
str = "";
return str;
}
public String dateToString(Date dt)
{
SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String str = "";
try
{
str = sdFormat.format(dt);
}
catch(Exception e)
{
String s = "";
return s;
}
if(str.equals("1900-01-01 00:00"))
str = "";
return str;
}
public String dateToString(Date dt, String strFormat)
{
SimpleDateFormat sdFormat = new SimpleDateFormat(strFormat);
String str = "";
try
{
str = sdFormat.format(dt);
}
catch(Exception e)
{
String s = "";
return s;
}
if(str.equals("1900-01-01 00:00"))
str = "";
return str;
}
public Date nextDay(Date dt)
{
Date nextDay = null;
nextDay = new Date(dt.getYear(), dt.getMonth(), dt.getDate() + 1);
return nextDay;
}
public Date dayAfter(Date dt, int days)
{
Date nextDay = null;
nextDay = new Date(dt.getYear(), dt.getMonth(), dt.getDate() + days);
return nextDay;
}
public Date dayBefore(Date dt, int days)
{
Date nextDay = null;
nextDay = new Date(dt.getYear(), dt.getMonth(), dt.getDate() - days);
return nextDay;
}
public Date dayAfter(Date dt, int interval, int format)
{
Date nextDay = null;
switch(format)
{
case 1: // '\001'
nextDay = new Date(dt.getYear() + interval, dt.getMonth(), dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds());
break;
case 2: // '\002'
nextDay = new Date(dt.getYear(), dt.getMonth() + interval, dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds());
break;
case 3: // '\003'
nextDay = new Date(dt.getYear(), dt.getMonth(), dt.getDate() + interval, dt.getHours(), dt.getMinutes(), dt.getSeconds());
break;
case 4: // '\004'
nextDay = new Date(dt.getYear(), dt.getMonth(), dt.getDate(), dt.getHours() + interval, dt.getMinutes(), dt.getSeconds());
break;
case 5: // '\005'
nextDay = new Date(dt.getYear(), dt.getMonth(), dt.getDate(), dt.getHours(), dt.getMinutes() + interval, dt.getSeconds());
break;
case 6: // '\006'
nextDay = new Date(dt.getYear(), dt.getMonth(), dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds() + interval);
break;
}
return nextDay;
}
public String dateToWeek(Date dt)
{
int iDay = dt.getDay();
String temp = "";
switch(iDay)
{
case 0: // '\0'
temp = "星期天";
break;
case 1: // '\001'
temp = "星期一";
break;
case 2: // '\002'
temp = "星期二";
break;
case 3: // '\003'
temp = "星期三";
break;
case 4: // '\004'
temp = "星期四";
break;
case 5: // '\005'
temp = "星期五";
break;
case 6: // '\006'
temp = "星期六";
break;
default:
temp = "";
break;
}
return temp;
}
public String dayToString(int iDay)
{
String temp = "";
switch(iDay)
{
case 0: // '\0'
temp = "星期天";
break;
case 1: // '\001'
temp = "星期一";
break;
case 2: // '\002'
temp = "星期二";
break;
case 3: // '\003'
temp = "星期三";
break;
case 4: // '\004'
temp = "星期四";
break;
case 5: // '\005'
temp = "星期五";
break;
case 6: // '\006'
temp = "星期六";
break;
default:
temp = "";
break;
}
return temp;
}
public int interval(Date first, Date last)
{
int inter = 0;
Date dt_first = stringToDate(dateToYMD(first));
Date dt_last = stringToDate(dateToYMD(last));
inter = (int)((dt_last.getTime() - dt_first.getTime()) / (long)0x5265c00);
return inter;
}
public static void main(String args[])
{
ConvertDate convertDate = new ConvertDate();
System.out.println(convertDate.stringToDate("2007-01-10"));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -