zoukankan      html  css  js  c++  java
  • 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();
     
    7.demo:
    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);
    
    Calendar转换String:
      Calendar currentCal = Calendar.getInstance();
      SimpleDateFormat currentSdf = new SimpleDateFormat("yyyy-MM-dd");
      String currentCalTime = currentSdf.format(currentCal.getTime());
  • 相关阅读:
    隐藏导航练习
    分层导航
    做一个问题,如果输入的答案正确则弹出正确,错误弹出错误
    同意按钮,倒计时10秒
    golang strings
    seek指针大文件上传
    go文件操作大全
    zipimport.ZipImportError: can't decompress data; zlib not available 解决办法
    centos 安装redis自启动要点
    golang 文件读取
  • 原文地址:https://www.cnblogs.com/loong-hon/p/11255913.html
Copyright © 2011-2022 走看看