java时间操作
1.2种常见时间操作:
import java.util.Date; import java.util.Calendar; import java.text.SimpleDateFormat; public class TestDate{ public static void main(String[] args){ Date now = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式 String hehe = dateFormat.format( now ); System.out.println(hehe); 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); } } 有时候要把String类型的时间转换为Date类型,通过以下的方式,就可以将你刚得到的时间字符串转换为Date类型了。 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); java.util.Date time=null; try { time= sdf.parse(sdf.format(new Date())); } catch (ParseException e) { e.printStackTrace(); }
2.日期比较:
Calendar c = Calendar.getInstance(); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); DateFormat df = new SimpleDateFormat("HH:mm"); Date d1 = df.parse("11:31");//字符串转日期 Date d2 = df.parse("11:30"); long diff = d1.getTime() - d2.getTime();//这里diff是一个长整形数,如果d1比d2靠后,就是正数,比如本例就是正数,反之为复数,相等为0 System.out.println(diff);