- 4
- 0
- 约6.57千字
- 约 7页
- 2017-06-05 发布于湖北
- 举报
日期及时间
java.text.SimpleDateFormat
java.text.Date
java.text.Calendar
时间比较
时 间 是 越 往 后 值 越 大
时间类
GregorianCalendar
此类继承Calendar(抽象类)。获得日期时间的方法:
类对象.get(Calendar.YEAR)获得年,
类对象.get(Calendar.AM_PM) 获得上午还是下午,0为AM,1为PM,12点
均是0
类对象.get(Calendar.DAY_OF_WEEK) 获得星期,周日为1,周六为7
类对象.get(Calendar.MONTH) 获得月值为0-11,所以应当再加1
其它的类推,查看Calendar类的Field Summary
日期时间运用
获得当前时间(毫秒)
long nowsencond = System.currentTimeMillis()
获得当前日期
System.out.println(new Date()) 如Sun Mar 09 09:56:04 CST 2008
获得前一天日期
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, -1);
date = calendar.getTime();
//SimpleDateFormat format = new SimpleDateFormat();
//format.applyPattern(yyyy-MM-dd);
//String strDate = format.format(date);
//System.out.println( strDate);
获得后一天日期
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, 1);
date = calendar.getTime();
时间加减
a)先将所两个时间转化为long型
Date time = new Date()
SimpleDateFormat dateFormat = new SimpleDateFormat(yyyyMMddHHmmssSSS)
long timeLong = Long.parseLong(dateFormat.format(time ))
b)将两个毫秒数时间进行加减,然后把long再封装成Data
Date result= new Date(相加或相减后的毫秒数)
例:得到当前时间之后90天的毫秒数
long endsencond = System.currentTimeMillis() + 90L * 24L * 60L * 60L * 1000L
日期转化为字符串
Date date = new Date() //得到当前的日期Date
String strDate =
1、SimpleDateFormat构造式
SimpleDateFormat dateFormat = new SimpleDateFormat(yyyyMMddHHmmssSSS)
2、SimpleDateFormat引入式
SimpleDateFormat format = new SimpleDateFormat();
format.applyPattern(yyyy-MM-dd HH:mm:ss);
strDate = format.format(date);
System.out.println(strDate)
输出格式:2008-03-09 09:56:04
format.applyPattern(yyyy-MM-dd);
strDate = format.format(date);
System.out.println(strDate)
输出格式:2008-03-09
format.applyPattern(yyyyMMddHHmmss);
strDate = format.format(date);
System.out.println(strDate)
输出格式:20080309095604
format.applyPattern(yyyy年MM月d
原创力文档

文档评论(0)