package com.hilary.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(StrToDate("2012-06-28 19:50:48"));;
System.out.println(DateToStr(new Date()));
}
/**
* 日期转字符串
* @author hilary
* @data 2012-6-28
* @param date
* @return
*/
public static String DateToStr(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = format.format(date);
return str;
}
/**
* 字符串转日期
* @author hilary
* @data 2012-6-28
* @param str
* @return
*/
public static Date StrToDate(String str) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
/**
* 日期字符串转换成时间戳
* @author hilary
* @data 2012-6-28
* @param dateString
* @return
*/
public static long dateToLong(String dateString) {
Date date = null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
date = format.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
return date.getTime();
}