//示例代码:
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
public class InttoDate {
/**
* 数字类型转换为日期类型
* @param year
* @param month
* @param date
* @param hourOfDay
* @param minute
* @param second
* @return
*/
public Date inttodate(int year, int month, int date, int hourOfDay, int minute, int second){
Calendar calendar=Calendar.getInstance();
calendar.set(year, month-1, date, hourOfDay, minute, second);
return calendar.getTime();
}
/**
* 日期类型转换为数字类型
* @return
*/
public String datetoint(){
Calendar calendar=Calendar.getInstance();
int year=calendar.get(Calendar.YEAR);
int month=calendar.get(Calendar.MONTH)+1;
int date=calendar.get(Calendar.DATE);
int hour=calendar.get(Calendar.HOUR_OF_DAY);
int minute=calendar.get(Calendar.MINUTE);
int second=calendar.get(Calendar.SECOND);
return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
}
/**
* 字符串类型转换为日期类型
* @param dateString
* @return
*/
public Date stringtodate(String dateString){
DateFormat dateFormat;
Date date=null;
try{
dateFormat=DateFormat.getDateInstance();
date=dateFormat.parse(dateString);
}catch(Exception e){
e.printStackTrace();
}
return date;
}
/**
* 日期类型转换为字符串类型
* @param date
* @return
*/
public String datetostring(Date date){
String str=date.toString();
str=date.toLocaleString();
return str;
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(new InttoDate().inttodate(2007,8, 5, 3, 11, 55));
System.out.println(new InttoDate().datetoint());
System.out.println(new InttoDate().stringtodate("2007-8-5 5:49:47"));
System.out.println(new InttoDate().datetostring(new Date()));
}
}
|