1.
SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datetime = tempDate.format(new java.util.Date());
//也可以用这个
// String datetime = tempDate.format(new Date(System.currentTimeMillis()));
2. 使用Calendar
Calendar c = Calendar.getInstance();//可以用set()对每个时间域单独修改
int year = c.get(Calendar.YEAR);
//一般month都需要+1才表示当前月份
int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
String nowTime = year + "/" + month + "/" + date + " " +hour + ":" + minute + ":" + second;
本文深入探讨了Java中日期时间的两种常用处理方式:SimpleDateFormat和Calendar,通过实例演示如何格式化日期时间字符串以及如何使用Calendar进行时间的单独修改和获取。
9534

被折叠的 条评论
为什么被折叠?



