zoukankan      html  css  js  c++  java
  • Date、String、Calendar类型之间的转化

    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();
     
    下面有个例子,插入oralce表类型为DATE的数据
     
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmmss");
    String date = sdf.format(new Date());
    -----------------------------------------
    String sql = "Insert into SM_SEND (*****************) " +
    "values (*******************************
    "',1," + "to_date('" +date+ "','YYYY-MM-DD HH24:MI:SS')" +
    "," + "to_date('" +date+ "','YYYY-MM-DD HH24:MI:SS')" +
    ",0,null,null,null,null,null,null,null)";
    注:date转calendar时使用calendar.setTime();
    有时当setTime的参数格式问题会导致转换异常
     
    实例 1
    /**
         * 获取上月十五日凌晨时间
         * @author:  WY
         * @rise:
         */
        public static String previousMonth15() throws ParseException{
            
            Calendar calendar = Calendar.getInstance();
            
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH)-1);
            calendar.set(Calendar.DAY_OF_MONTH,15);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.SECOND,0);
            calendar.set(Calendar.MINUTE,0);
            
            Date strDateTo = calendar.getTime();      
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            
            return     sdf.format(strDateTo);
        }
  • 相关阅读:
    H5实现魔方游戏
    T-SQL:CTE用法(十)
    c# API接收Base64转图片
    T-SQL :联接查询练习 (杂)
    T-SQL:基础练习(杂)
    UI5-文档-导航栏
    UI5-文档-4.10-Descriptor for Applications
    UI5-文档-4.9-Component Configuration
    UI5-文档-4.8-Translatable Texts
    UI5-文档-4.7-JSON Model
  • 原文地址:https://www.cnblogs.com/WangYunShuaiBaoLe/p/8038849.html
Copyright © 2011-2022 走看看