1.通过Calendar获取时间
final Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime(); // Wed Aug 29 02:45:00 格林尼治标准时间+0000 2012
String gmt = date.toGMTString(); // 29 Aug 2012 02:45:00 GMT
String loc = date.toLocaleString(); // 2012-8-29 上午02:45:00
2.通过SimpleDateFormat格式化时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String yymmdd = simpleDateFormat.format(date); // 2012-08-29 02:45:00
3.字符串转SimpleDateFormat
String strDate="2005年04月22日";
//注意:SimpleDateFormat构造函数的样式与strDate的样式必须相符
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy年MM月dd日");
Date date1 = null;
try {
date1 = simpleDateFormat1.parse(strDate); // Fri Apr 22 00:00:00 格林尼治标准时间+0000 2005
} catch (ParseException e) {
e.printStackTrace();
}
4.将毫秒数换转成日期类型
long now = System.currentTimeMillis(); // 1346208300247
Date dNow = new Date(now); // Wed Aug 29 02:45:00 格林尼治标准时间+0000 2012
5.计算据之后的某一天还剩多长时间
long now = System.currentTimeMillis(); // 获取当前微妙
final Calendar calendar = Calendar.getInstance();
calendar.set(2012, 7, 30, 3, 35, 45); // 设置未来的某一天,注意"月份"7就是实际的8月
long next = calendar.getTimeInMillis();
long ss = (next - now)/1000; // 忽略毫秒,得到秒
long day = ss / (60*60*24); // 天
ss %= (60*60*24);
long hour = ss / (60*60); // 小时
ss %= (60*60);
long min = ss / 60; // 分钟
ss %= 60; // 秒