import java.util.*;
import java.text.*;public class C
{
static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD");
public static void main(String[] args) throws Exception
{
System.out.println(sdf.parse("1989-08-02"));
}
}//为什么会输出Mon Jan 02 00:00:00 CST 1989 如何让他正确的输出日期?
import java.util.*;import java.text.*;
import java.util.*;public class DateTool { public static void main(String[] args){
String t = "2008-03-05 16:02:03.032";
DateFormat longFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date d1 = null;
Date d2 = null;
try {
d1 = longFormat.parse(t);
d2 = sdf.parse(t);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(longFormat.format(d1));
System.out.println(sdf.format(d2));
}
}
SimpleDateFormat 输出的格式:2008-03-05 16:02:03.032
/***************************************************************************
* 计算两个时间间隔的秒数
*
* @param time1
* 格式为yyyy-MM-dd HH:mm:ss
* @param time2
* 格式为yyyy-MM-dd HH:mm:ss
* @return
*/
public static long calcBetweenSecond(String time1, String time2) {long second = 0;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date d1 = format.parse(time1);
Date d2 = format.parse(time2);
second = (d2.getTime() - d1.getTime()) / 1000;
} catch (java.text.ParseException e) {
}
return second;
}
/**
* 有毫秒数返回格式化的时间字符串
* @param time
* @return
*/
public static String formatHHmmss(long time){
Date date = new Date(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
return sdf.format(date);
}
/**
* 输入指定的时间字符串返回Date类型的字符串
* @param dateStr
* @return Date
*/
public static Date formateStringToDate(String dateStr) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date;
try {
date = format.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
return date;
}
/**
* 将时间戳格式化为的时间格式
* @param total
* @return String
*/
public static String getTimeLongToString(long total) {
if (total <= 0)
return "00:00:00";
long hour = total / 3600;
long min = (total % 3600) / 60;
long seconds = ((total % 3600) % 60);
String str = new String();
if (hour >= 0 && hour < 10)
str += "0" + hour + ":";
else
str += hour + ":";
if (min >= 0 && min < 10)
str += "0" + min + ":";
else
str += min + ":";
if (seconds >= 0 && seconds < 10)
str += "0" + seconds;
else
str += seconds;
return str;
}
/**
* 将二进制的字符串格式化为日期类型
* @param baseDate
* @return
*/
public static Calendar parseDateTime(String baseDate) {
Calendar cal = null;
cal = new GregorianCalendar();
int yy = Integer.parseInt(baseDate.substring(0, 4));
int mm = Integer.parseInt(baseDate.substring(5, 7)) - 1;
int dd = Integer.parseInt(baseDate.substring(8, 10));
int hh = 0;
int mi = 0;
int ss = 0;
if (baseDate.length() > 12) {
hh = Integer.parseInt(baseDate.substring(11, 13));
mi = Integer.parseInt(baseDate.substring(14, 16));
ss = Integer.parseInt(baseDate.substring(17, 19));
}
cal.set(yy, mm, dd, hh, mi, ss);
return cal;
} /**
* 由日期字符串取得日期类型
* @param strDate
* @return
*/
public int getDay(String strDate) {
Calendar cal = parseDateTime(strDate);
return cal.get(Calendar.DATE);
}
/**
* 求两日期相差天数(日期想减)
* @param d1
* @param d2
* @return
*/
public static long minusDate(Date d1, Date d2) {
long i = -99;
System.out.println(d1 + "" + d1.getTime());
System.out.println(d2 + "" + d2.getTime());
long temp = d2.getTime() - d1.getTime();
System.out.println(temp);
i = temp / (1000 * 60 * 60 * 24);
return i;
}
/**
* @see 取得当前日期并格式为:yyyy-MM-dd)
* @return String
*/
public String GetDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String sDate = sdf.format(new Date());
return sDate;
} /**
* @see 取得当前时间(格式为:yyy-MM-dd HH:mm:ss)
* @return String
*/
public static String GetDateTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sDate = sdf.format(new Date());
return sDate;
}
public String FormatDateTime(String strDateTime, String strFormat) {
String sDateTime = strDateTime;
try {
Calendar Cal = parseDateTime(strDateTime);
SimpleDateFormat sdf = null;
sdf = new SimpleDateFormat(strFormat);
sDateTime = sdf.format(Cal.getTime());
} catch (Exception e) { }
return sDateTime;
} /**
* 由日期字符串得到约数加一
* @param strDate
* @return
*/
public int getMonth(String strDate) {
Calendar cal = parseDateTime(strDate);
return cal.get(Calendar.MONTH) + 1;
}
/**
* 输入特定的日期获得相应的毫秒数
*
*/
public void getTimeMillis(){
Calendar c1 = new GregorianCalendar(2008,7,8,20,8,8);
c1.getTime();
Calendar c2 = new GregorianCalendar(2010,01,04,11,49,43);
c2.getTimeInMillis();
}
/**
* 有日期字符串返回一周的第几天
* @param strDate
* @return
*/
public int getWeekDay(String strDate) {
Calendar cal = parseDateTime(strDate);
return cal.get(Calendar.DAY_OF_WEEK);
}
/***************************************************************************
* @功能 判断某年是否为闰年
* @return boolean
* @throws ParseException
**************************************************************************/
public boolean isLeapYear(int yearNum) {
boolean isLeep = false;
/** 判断是否为闰年,赋值给一标识符flag */
if ((yearNum % 4 == 0) && (yearNum % 100 != 0)) {
isLeep = true;
} else if (yearNum % 400 == 0) {
isLeep = true;
} else {
isLeep = false;
}
return isLeep;
} public static String miuDate(Date date, int day) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_YEAR, day);
// cal.roll(Calendar.DAY_OF_YEAR, day);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date d = cal.getTime();
return format.format(d);
} public static String getLastDayOfMonth() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.DAY_OF_MONTH, cal
.getActualMaximum(Calendar.DAY_OF_MONTH));
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(cal.getTime());
} /**
* 取当前打印日期
*
* @return
* @throws java.lang.Exception
*/
public String getPrintDate() {
String printDate = "";
Calendar calendar = new GregorianCalendar();
calendar.setTime(new Date());
printDate += calendar.get(Calendar.YEAR) + "年";
printDate += (calendar.get(Calendar.MONTH) + 1) + "月";
printDate += calendar.get(Calendar.DATE) + "日";
return printDate;
} /**
* 将指定的日期字符串转化为日期对象
*
* @param dateStr
* 日期字符串
* @return java.util.Date
*/
public static Date getDate(String dateStr, String format) throws Exception { if (dateStr == null || format == null) {
throw new Exception("数据类型异常" + dateStr + "|" + format);
} SimpleDateFormat df = new SimpleDateFormat(format); try {
Date date = df.parse(dateStr);
return date;
} catch (Exception ex) {
return null;
}
} /**
* 将指定日期转换为 Timestamp
*
* @param date
* 指定日期格式为 "20030908"
* @return Timestamp
* @throws Exception
*/
public static Timestamp getTimestamp(String dateStr) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00.000");
return Timestamp.valueOf(sdf.format(getDate(dateStr, "yyyyMMdd")));
} /**
* 从指定Timestamp中得到相应的日期
*
* @param datetime
* 指定的Timestamp
* @return 日期 "2003-09-08"
*/
public String getDateFromDateTime(Timestamp datetime) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.format(datetime).toString();
} /**
* 得到当前时间的时间戳
*
* @return 当前时间戳
*/
public Timestamp getNowTimestamp() {
long curTime = System.currentTimeMillis();
return new Timestamp(curTime);
} public static String getFirstDayOfMonth() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.DATE, 1);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(cal.getTime());
}
}自己挑吧!!
需要哪个就用哪个吧~~~~ 嘿嘿~~~~