日期時間處理

public class date_test {

  public static void main(String args[]){
    makeUDateObject(); //產生 util.Date
    makeSDateObject(); //產生 sql.Date
    DateObjectsTran(); //日期相關 Object互轉
    
    getNowTimestamp(); //取得目前時間的 Timestamp long
    
    
    timestamp2StringDate(System.currentTimeMillis(), "yyyy-MM-dd" ); //Timestamp 轉 日期字串
    StringDate2timestamp("2012-01-01"); //日期字串 轉 Timestamp
    StringDate2YMD("2012-05-01"); //日期字串 轉 Y,M,D
    
    CountDiffDate("2012-03-01","2012-04-01"); //計算兩日期差異值
    addDate(); //增減日期
    compareDate(); //日期比較(相等、較早、較晚)
  }




  /**
   * 產生Util Date object 
   */
  private static void makeUDateObject() {
    System.out.println("=取得 uDate =================");

    //方法1, 用 Timestamp 把 string 轉 date object 
    java.util.Date d1 = new java.util.Date(Timestamp.valueOf("2012-05-01 00:00:00").getTime());
    System.out.println("Timestamp to Date objcet =>"+d1.toString());
    
    //方法2, 用 SimpleDateFormat 把 String Date 轉 date object
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.TAIWAN);
    java.util.Date d2 = new java.util.Date();
    try {
      d2= sdf.parse("2012-05-01");
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    System.out.println("String use SimpleDateFormate to Date objcet =>"+d2.toString());

        System.out.println("");    
  }

  /**
   * 產生Sql Date object 
   */
  private static void makeSDateObject() {
    System.out.println("=取得 sDate =================");

    //方法1, 用 Timestamp 把 string 轉 date object 
    java.sql.Date d1 = new java.sql.Date(Timestamp.valueOf("2012-05-01 00:00:00").getTime());
    System.out.println("Timestamp to Date objcet =>"+d1.toString());
      
    //方法2, String 轉 date object
    java.sql.Date d2 = java.sql.Date.valueOf("2012-05-01");  //不能有時分秒
    System.out.println("String use sDate.valueOf to Date objcet =>"+d2.toString());
    
        System.out.println("");    
  }

  /**
   * Util Date 轉 Sql Date
   */
  private static void DateObjectsTran() {
    java.util.Date uDate = new java.util.Date(Timestamp.valueOf("2012-05-01 00:00:00").getTime());
    java.sql.Date sDate = java.sql.Date.valueOf("2012-05-01");
    
    System.out.println("=Util,Sql Date 互轉  =================");
    // java.util.Date --> java.sql.Date
    java.sql.Date sDate1 = new java.sql.Date(uDate.getTime());
    // java.sql.Date --> java.util.Date 
    java.util.Date uDate1 = new java.util.Date(sDate.getTime());
    System.out.println("use getTime() =>"+sDate1.toString()+"|"+uDate1.toString());

        System.out.println("");
        
    System.out.println("=Util,Calendar 互轉  =================");
    // java.util.Date --> java.util.Calendar
    Calendar cal = Calendar.getInstance(Locale.TAIWAN);
    cal.setTime(uDate);
    System.out.println("use cal.setTime() to Calendar =>"+cal.getTimeInMillis());
    // java.util.Calendar --> java.util.Date
    java.util.Date uDate2 = cal.getTime();
    System.out.println("use cal.getTime() to uDate =>"+uDate2.getTime());
    
    
        System.out.println("");
  }

  /**
   * 取得現在時間 Timestamp
   */
  private static void getNowTimestamp() {
    System.out.println("=取得現在時間 Timestamp =================");
    //方法1, System
    System.out.println("System.currentTimeMillis() =>"+System.currentTimeMillis());

    //方法2, Calendar
    Calendar c = Calendar.getInstance(Locale.TAIWAN);
    System.out.println("Calendar.getTime() =>"+c.getTime());
    System.out.println("Calendar.getTimeInMillis() =>"+c.getTimeInMillis());
    
    //方法3, util.Date
    System.out.println("java.util.Date().getTime() =>"+ new java.util.Date().getTime());
    
    //方法4, util.Date + timestamp
    java.util.Date uDate = new java.util.Date();
    java.sql.Timestamp ts = new java.sql.Timestamp(uDate.getTime());
    System.out.println("timestamp.getTime() =>"+ts.getTime());
    
        System.out.println("");
  }

  /**
   * timestamp 轉 String Date
   * @param tstamp
   * @param DATE_FORMAT
   */
  private static void timestamp2StringDate(long tstamp, String DATE_FORMAT) {

    System.out.println("=timestamp 轉 String Date============");
    //java timestamp 單位為"微秒",與 php 的 timestamp 多 1000倍
    
    //方法1, sql.Date
    java.sql.Date sDate = new java.sql.Date(tstamp);
    System.out.println("sDate.toString=>"+sDate.toString());
    
    //方法2, SimpleDateFormat
    java.util.Date uDate = new java.util.Date(tstamp);        
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT,Locale.TAIWAN);
        System.out.println("simpleDateFormat.format(uDate)=>"+sdf.format(uDate));
        System.out.println("simpleDateFormat.format(sDate)=>"+sdf.format(sDate));
        
        //方法3, date String (只能取得全格式,要單一取需經過 SimpleDateFormat)
        java.util.Date uDate2 = new java.util.Date(tstamp);
        System.out.println("uDate.toString=>"+uDate2.toString());
        
        System.out.println("");
  }

  /**
   * String Date 轉 timestamp
   * @param string
   */
  private static void StringDate2timestamp(String stringDate) {
    System.out.println("=String Date 轉 timestamp============");
    //方法1, Timestamp (String 格式必須包含時分秒 yyyy-mm-dd hh:mm:ss)
    System.out.println("Timestamp.valueOf=>"+Timestamp.valueOf(stringDate+" 00:00:00").getTime());

    //方法2, Calendar.set 
    String[] d = stringDate.split("-");
    Calendar cal = Calendar.getInstance(Locale.TAIWAN);
    cal.clear();  //務必先清除 (clear 之後好像要設定什麼預設值)
    cal.set(Integer.valueOf(d[0]), (Integer.valueOf(d[1])-1), Integer.valueOf(d[2]));
    
    System.out.println("Calendar.set=>"+cal.getTimeInMillis());
        System.out.println("");
  }

  /**
   * String Date 轉 YMD
   * @param string
   */
  private static void StringDate2YMD(String stringDate) {
    System.out.println("=String Date 轉 YMD============");
        Calendar cal = Calendar.getInstance(Locale.TAIWAN);
        cal.setTime(java.sql.Date.valueOf(stringDate));
        //月份請 +1
        System.out.println("use cal.get(Calendar.YEAR) =>年=>" + cal.get(Calendar.YEAR) + "月" + (cal.get(Calendar.MONTH)+1) + "日" +cal.get(Calendar.DATE));

        System.out.println("");    
  }
  
  
  /**
   * 計算時間差
   */
  private static void CountDiffDate(String date1, String date2) {
    System.out.println("=計算時間差(日期)============");
    
    long diff = java.sql.Date.valueOf(date2).getTime() - java.sql.Date.valueOf(date1).getTime();
    long dates = diff/(1000*60*60*24);
    System.out.println(date2 + " 與 " + date1 + " 差 " + dates + "天");
        
    Calendar cal1 = Calendar.getInstance(Locale.TAIWAN);

        System.out.println("");    
  }

  /**
   * 加減日期
   */
  private static void addDate() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    
    System.out.println("=加減日期============");
    Calendar cal = Calendar.getInstance(Locale.TAIWAN);
    cal.setTime(java.sql.Date.valueOf("2012-05-01"));
    
    //調整日(自動進位)
    cal.add(Calendar.DATE,+1);
    System.out.println("cal.add +1 date=>"+sdf.format(cal.getTime()));
    cal.add(Calendar.DATE,-2);
    System.out.println("cal.add -1 date=>"+sdf.format(cal.getTime()));
    
    //回歸
    cal.add(Calendar.DATE,+1);
    
    //調整日(不自動進位)
    cal.roll(Calendar.DATE,+1);
    System.out.println("cal.roll +1 date=>"+sdf.format(cal.getTime()));
    cal.roll(Calendar.DATE,-2);
    System.out.println("cal.roll -1 date=>"+sdf.format(cal.getTime()));
    
        System.out.println("");
  }
  

  /**
   * 日期比較
   */
  private static void compareDate() {

    java.sql.Date sDate1 = java.sql.Date.valueOf("2012-05-01");
    java.sql.Date sDate2 = java.sql.Date.valueOf("2012-02-01");

    System.out.println("Date1="+sDate1+",Date2="+sDate2);
    System.out.println("較晚 Date1.after Date2 =>"+sDate1.after(sDate2));
    System.out.println("較早 Date1.before Date2 =>"+sDate1.before(sDate2));
    System.out.println("相同 Date1.equals Date2 =>"+sDate1.equals(sDate2));

        System.out.println("");
  }
}
Date
  • make Date object
  • java timestamp -> string date v
  • string date->  java timestamp v
  • string date -> year , month , day v
  • year, month, day -> string date v(直接用 - 接起來)
  • string date +- diff date 
  • string date count diff date

 Time
  • java timestamp -> string time
  • string time -> java timestamp
  • string time -> hour, min, sec
  • hour, min, sec ->string time
  • string time +- diff time
  • string time count diff time
Date對象
日期時間處理 日期時間處理 Reviewed by Wild on 8/23/2012 02:43:00 下午 Rating: 5

沒有留言:

沒有Google帳號也可發表意見唷!

技術提供:Blogger.