在开发中常使用情况。
1、将String转为date 例如"201604131630"
//设置日期格式
public SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public SimpleDateFormat sdfOrignal = new SimpleDateFormat("yyyyMMddHHmm");
String startTimeS = "201604131630";
long startTime = sdfOrignal.parse(startTimeS).getTime();
String stringStartTime = sdf.format(new Date(startTime))
2、将当前系统时间转为String
//设置日期格式
public SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//new Date()为获取当前系统时间
String date = sf.format(new Date());
3、Calendar 的使用。
Calendar 设置数据测试:
//设置日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
Calendar calendar = Calendar.getInstance(); //测试数据为13日16:30数据 calendar.set(Calendar.DAY_OF_MONTH, 13); calendar.set(Calendar.HOUR_OF_DAY, 16); calendar.set(Calendar.MINUTE, 30); calendar.set(Calendar.MILLISECOND, 0); long lastTime = calendar.getTimeInMillis()/60000; String fmtTime = sdf.format(new Date(lastTime*60000))
Calendar 获取数据测试:
Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
int year = c.get(Calendar.YEAR); 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); System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);
就写这么多吧。。。。