1、日期相减
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date now = null; Date date = null; try { now = df.parse("2004-03-26 13:31:34"); date=df.parse("2004-01-02 11:30:53"); long l=now.getTime()-date.getTime(); long day=l/(24*60*60*1000); long hour=(l/(60*60*1000)-day*24); long min=((l/(60*1000))-day*24*60-hour*60); long s=(l/1000-day*24*60*60-hour*60*60-min*60); Log.d("=========", day + "天" + hour + "小时" + min + "分" + s + "秒"); } catch (ParseException e) { e.printStackTrace(); }
2、日期转字符串
Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd E HH:mm:ss"); String time = sdf.format(date); Log.d("=====", time);
3、字符串转日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = sdf.parse( "2008-07-10 19:20:00" ); } catch (ParseException e) { e.printStackTrace(); } Calendar c = Calendar.getInstance(); c.setTime(date); int i = c.get(Calendar.YEAR); Log.d("=======",i+"");