yxhxj2006

          常用鏈接

          統計

          最新評論

          String、data、Calendar、Timestamp 之間的轉換


          String、data、Calendar、Timestamp 之間的轉換 

          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          1.Calendar 轉化 String 
          //獲取當前時間的具體情況,如年,月,日,week,date,分,秒等 
                  Calendar calendat = Calendar.getInstance();
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
          String dateStr = sdf.format(calendar.getTime());

          2.String 轉化Calendar
          String str="2010-5-27";
          SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
          Date date =sdf.parse(str);
          Calendar calendar = Calendar.getInstance();
          calendar.setTime(date);

          3.Date 轉化String
          SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
          String dateStr=sdf.format(new Date());

          4.String 轉化Date
          String str="2010-5-27";
          SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
          Date birthday = sdf.parse(str);

          5.Date 轉化Calendar
          Calendar calendar = Calendar.getInstance();
          calendar.setTime(new java.util.Date());

          6.Calendar轉化Date
          Calendar calendar = Calendar.getInstance();
          java.util.Date date =calendar.getTime();

          7.Date 轉成 String
          System.out.println(sdf.format(new Date())); 
          8.String 轉成 Timestamp
          Timestamp ts = Timestamp.valueOf("2011-1-14 08:11:00");

          9.Timestamp 轉成 String
          sdf.format(ts);

          Timestamp和Date多數用法是一樣的。
          10.Date 轉 TimeStamp
          SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          String time = df.format(new Date());
          Timestamp ts = Timestamp.valueOf(time);

          11.日期比較大小
          String ti="2010-11-25 20:11:00";
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
          Date time=sdf.parse(ti);
          String ti2="2011-11-26 20:11:00";
          Date time2=sdf.parse(ti2);
          int c=ti2.compareTo(ti);
          if(c>0){
              System.out.println(ti+"大");
          }else if(c==0){
              System.out.println("一樣大");
          }else{
              System.out.println(ti2+"大");
          }

          12.Date/ Timestamp 轉成 Calendar 
          Calendar calendar = Calendar.getInstance();
          calendar.setTime(startDate);
          calendar.add(Calendar.YEAR, 2);   //日期加2年
          System.out.println(calendar.getTime());
          calendar.add(Calendar.DATE, -30);     //日期加30天
          System.out.println(calendar.getTime());
          calendar.add(Calendar.MONTH, 3);  //日期加3個月
          System.out.println(calendar.getTime());
          Date date=new Date();
          DateFormat df=DateFormat.getDayeInstance();
          ps.setString(4,df.format(date))
          Date date = new Date(); 
          SimpleDateFormat from = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
          String times = from.format(date);
          public class Test1 {
          public static void main(String[] args) {
            long   m=System.currentTimeMillis();
            System.out.println(new Date(m) );
          }

          posted on 2013-04-15 14:38 奮斗成就男人 閱讀(283) 評論(0)  編輯  收藏 所屬分類: java

          主站蜘蛛池模板: 博乐市| 固始县| 鸡西市| 微山县| 盘山县| 镇雄县| 新泰市| 略阳县| 松原市| 娄底市| 那曲县| 五家渠市| 黔东| 锡林浩特市| 依安县| 怀宁县| 庄河市| 遵化市| 大同市| 无极县| 策勒县| 广元市| 罗源县| 松江区| 凌源市| 林周县| 东乡县| 红桥区| 新竹县| 建宁县| 延安市| 永嘉县| 吴堡县| 含山县| 会泽县| 宜兰市| 光山县| 华蓥市| 肥乡县| 昌邑市| 潜江市|