我遇到一个问题,就是SQL SERVER下的datetime字段
与JAVA类下的Date对象之家转化的问题,
例如数据库中是这样:‘2006-10-13 22:12:10’
的字符串,
与Date对象有个相互转化的问题,
例如我想把Date转化为上面的字符串形式,
还有就是把上面的这个字符串转化为Date对象,
怎以样作到呢?
急 !
/**
* @param str 格式要求:yyyy,m(mm),d(dd); 如:2002-01-02,2002-1-2,2002-2-15
* @return 成功返回日期,失败返回null;
*/
public static Date stringToDate(String str) {
String strFormat = "yyyy-MM-dd HH:mm";
if (str != null && str.length() == 10) {
strFormat = "yyyy-MM-dd";
}
SimpleDateFormat sdFormat = new SimpleDateFormat(strFormat);
Date date = new Date();
try {
date = sdFormat.parse(str);
}
catch(Exception e) {
//System.out.println("Error="+e);
return null;
}
return date;
}
public static Date stringToDate(String str, String strFormat) {
SimpleDateFormat sdFormat = new SimpleDateFormat(strFormat);
Date date = new Date();
try {
date = sdFormat.parse(str);
}
catch(Exception e) {
return null;
}
return date;
}
public static String dateToYMD(Date dt) {
SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
String str = "";
try {
str = sdFormat.format(dt);
}
catch(Exception e) {
return "";
}
if (str.equals("1900-01-01")) {
str = "";
}
return str;
}
public static String dateToString(Date dt) {
SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String str = "";
try {
str = sdFormat.format(dt);
}
catch(Exception e) {
return "";
}
if (str.equals("1900-01-01 00:00")) {
str = "";
}
return str;
}
public static String dateToString(Date dt, String strFormat) {
SimpleDateFormat sdFormat = new SimpleDateFormat(strFormat);
String str = "";
try {
str = sdFormat.format(dt);
}
catch(Exception e) {
return "";
}
if (str.equals("1900-01-01 00:00")) {
str = "";
}
return str;
}
把Date类转化成字符串:
Date date=new Date();
System.out.println(date.toLocalString());
把字符串转化成Date
String str="2006-10-13 22:12:10";
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date=dateFormat.parse(str);
=====================================================================================
/*
* DateUtil Class : 在于提供关于时间操作的一系列公用操作方法
* Create Date : 2004-2-9
* Version 1.0
*/
import java.sql.*;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class DateUtil {
public DateUtil() {
}
/**
* 根据格式获得日期字符串
* @param sFormat
* @return
*/
public static String getDateStr(String sFormat) {
Calendar tCal = Calendar.getInstance();
Timestamp ts = new Timestamp(tCal.getTime().getTime());
java.util.Date date = new java.util.Date(ts.getTime());
SimpleDateFormat formatter = new SimpleDateFormat(sFormat);
String tmpStr = formatter.format(date);
return (tmpStr);
}
/**
* 根据给定格式获取特定时间的格式化显示
* @param ts
* @param sFormat
* @return
*/
public static String getDateFormat(Timestamp ts, String sFormat) {
Date date = new Date(ts.getTime());
SimpleDateFormat formatter
= new SimpleDateFormat(sFormat);
String tmpStr = formatter.format(date);
return tmpStr;
}
/**
* 格式化日期
* @param ts
* @return
*/
public static String getSDate(Timestamp ts) {
Date date = new Date(ts.getTime());
SimpleDateFormat formatter
= new SimpleDateFormat("yyyy-MM-dd");
String tmpStr = formatter.format(date);
return tmpStr;
}
/**
* 将String类型的日期转换为时间
* @param dt
* @return
*/
public static Timestamp getTs(String dt) {
Date date = java.sql.Date.valueOf(dt);
Calendar tCal = Calendar.getInstance();
tCal.setTime(date);
Timestamp ts = new Timestamp(tCal.getTime().getTime());
return ts;
}
/**
* 建议获得短日期的处理方式
* 例如: getShortDate(2004-10-10 10:10:10.123) = 2004-10-10
* @param dt
* @return
*/
public static String getShortDate(String dt) {
try {
return dt.substring(0, dt.indexOf(" "));
}
catch (Exception e) {
return dt;
}
}
/**
*
* 取得当前日期时间
* @return
*/
public static Timestamp getCurrDateTime() {
Calendar tCal = Calendar.getInstance();
Timestamp createDate = new Timestamp(tCal.getTime().getTime());
return createDate;
}
/**
* 获得最常见的日期格式内容 : 年-月-日 小时-分钟-秒
* @param ts
* @return
*/
public static String getSDateTime(Timestamp ts) {
return getDateFormat(ts, "yyyy-mm-dd HH:mm:ss");
}
/*格式化日期*/
public static String getSTime(Timestamp ts) {
return getDateFormat(ts, "HH:mm:ss");
}
/**
* 获取当天的日期
* @return
*/
public static String getToday() {
java.sql.Timestamp ts = new java.sql.Timestamp(System.currentTimeMillis());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(ts);
}
// 根据时间获得随机数
public static String getRnd()
{
Calendar tCal = Calendar.getInstance();
Timestamp ts = new Timestamp(tCal.getTime().getTime());
java.util.Date date = new java.util.Date(ts.getTime());
SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddhhmmss");
String tmpStr = formatter.format(date)+Math.round(Math.random()*1000+1);
return (tmpStr);
}
/**
* 计算日期之间的差值 2004-3-25 增加
* @param dt1
* @param dt2
* @return
*/
public static int dateDiff(Timestamp dt1, Timestamp dt2) {
long ldate1 = dt1.getTime();
long ldate2 = dt2.getTime();
return (int)((ldate2-ldate1) / (1000*60*60*24));
}
/**
* 获取明天的日期
* @return
*/
public static String getTomorrow() {
return getNextDay(getToday());
}
/**
* 获得当前日期的下一天
* @param date
* @return
*/
public static String getNextDay(String date) {
if (date == null || date.trim().length() == 0) {
return "";
}
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(f.parse(date));
}
catch (ParseException ex) {
return date;
}
calendar.add(5, 1);
return f.format(calendar.getTime());
}
/**
* LST
* num为正:当前日期后num天是返回值
* num为负:当前日期前num天是返回值
* 返回的日期的格式:yyyy-MM-dd
*/
public static String getTheDay(int num){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
GregorianCalendar gc = new GregorianCalendar();
gc.add(GregorianCalendar.DATE, num);
Date theday =gc.getTime();
return sdf.format(theday);
}
}