`
tiehuali2
  • 浏览: 32886 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

字符时间转为Date、获取当前年月日、Date和Calendar

阅读更多

字符时间转为Date

 

 static Date change(String sDate)

{
      Date d = new Date();
      DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
      try {
           d = format.parse(sDate);
      } catch (ParseException e) {
            e.printStackTrace();
      }
      return d;
 }

 

--------------------------------------------------------------------------------------------------------

 

获取当前年、月或日

 Calendar cal = new GregorianCalendar();

 

  int year = cal.get(Calendar.YEAR);// 年
  int month = cal.get(Calendar.MONTH )+1;//月
 
  int day = cal.get(Calendar.DATE);//日

用Date也要相应获取的方法,但是已过期,不建议使用。

 

--------------------------------------------------------------------------------------------------------

 

Date 转为Calendar:

 

Calendar cal = new GregorianCalendar();

Date d = new Date();

cal.setTime(d);

 

Calendar 转为 Date:

d = cal.getTime();

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics