午夜视频在线网站,日韩视频精品在线,中文字幕精品一区二区三区在线,在线播放精品,1024你懂我懂的旧版人,欧美日韩一级黄色片,一区二区三区在线观看视频

分享

Date與String之前的轉(zhuǎn)化問題

 jinzq 2007-09-24

我遇到一個問題,就是SQL SERVER下的datetime字段
與JAVA類下的Date對象之家轉(zhuǎn)化的問題,
例如數(shù)據(jù)庫中是這樣:‘2006-10-13 22:12:10’
的字符串,
Date對象有個相互轉(zhuǎn)化的問題,
例如我想把Date轉(zhuǎn)化為上面的字符串形式,
還有就是把上面的這個字符串轉(zhuǎn)化為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類轉(zhuǎn)化成字符串:
Date date=new Date();
System.out.println(date.toLocalString());

把字符串轉(zhuǎn)化成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 : 在于提供關(guān)于時間操作的一系列公用操作方法
 * 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() {

  }

  /**
   * 根據(jù)格式獲得日期字符串
   * @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);
  }

  /**
   * 根據(jù)給定格式獲取特定時間的格式化顯示
   * @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類型的日期轉(zhuǎn)換為時間
   * @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;
  }

  /**
   * 獲得最常見的日期格式內(nèi)容 : 年-月-日 小時-分鐘-秒
   * @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);
  }

  // 根據(jù)時間獲得隨機數(shù)
    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);
 }

http://blog.csdn.net/haydenwang8287/archive/2007/09/14/1785626.aspx
}

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多