时间日期类
来源:互联网 发布:捷易通软件 编辑:程序博客网 时间:2024/05/22 20:55
在开发中经常会用到时间和日期,前面学习System类时,其中currentTimeMillis方法可以返回当前时间的毫秒值,可是这些数据看起来很不清晰,怎么能让这些数据变的
很清晰,便于阅读呢?在查阅System类的currentTimeMillis方法时,其中让我们另请参见Date类,找到这个Date发现,类 Date 表示特定的瞬间,精确到毫秒。继续查阅
Date类的描述,发现Date拥有多个构造函数,只是部分已经过时,但是其中有未过时的构造函数可以把毫秒值转成日期对象。
//获取当前时间的毫秒值
long time = System.currentTimeMillis();
//创建日期对象,把当前的毫秒值转成日期对象
Date date = new Date(time);
System.out.println(date);//结果:Wed Dec 11 16:42:06 CST 2013
可是将毫秒值转成日期后,输出的格式不利于我们阅读,继续查阅API,Date中有getYear、getMouth等方法,可以他们已经过时,继续往下查阅,看到了toString方法,
点开查阅原来是上面打印的date就是默认调用了这个toString方法,并且在这个方法下面还有让我们参见toLocaleString方法,点进去,这个方法又过时了,从 JDK 1.1
开始,由 DateFormat.format(Date date) 取代。
既然这个方法被DateFormat.format(Date date) 取代,那么就要去查阅DateFormat类。
2、DateFormat类介绍
查阅DateFormat类介绍:DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。日期/时间格式化子类(如 SimpleDateFormat
)允许进行格式化(也就是日期 -> 文本)、解析(文本-> 日期)和标准化。
原来这个类可以帮我们完成日期和文本之间的转换。继续阅读API,DateFormat 可帮助进行格式化并解析任何语言环境的日期。对于月、星期,甚至日历格式(阴历和阳
历),其代码可完全与语言环境的约定无关。要格式化一个当前语言环境下的日期,可使用某个静态工厂方法:
myString = DateFormat.getDateInstance().format(myDate);
//获取日期格式器对象,在获取格式器对象时可以指定风格,风格包括 FULL、LONG、MEDIUM 和 SHORT
DateFormat format = DateFormat.getDateInstance(DateFormat.LONG);
//对日期进行格式化
String str_time = format.format(date);
System.out.println(str_time);//结果:2013年12月11日
3、Calendar类介绍
我们可以通过Date获取到日期对象,使用DateFormat方法可以把日期进行格式化,可我们想获取当前日期对应的具体的年,具体的月,具体的日,等信息,怎么获取呢?
找到Date继续查阅,其中有getYear等方法,只是这些方法过时了,并且这些方法的描述中告诉我们参加Calendar类。
查阅Calendar类的API描述,发现原来Calendar类是专门用于描述日历的类,它中维护了一个map集合,在这个集合中存放着所有和日期相关的信息。
public class DateDemo { public static void main(String[] args) { //获取日历对象 Calendar c = Calendar.getInstance(); System.out.println(c);//打印出了一个map集合,其中描述日期的所有信息 //获取年 int year = c.get(Calendar.YEAR); //获取月,月是从0开始计算的,所以应该“+1” int mouth = c.get(Calendar.MONTH)+1; //获取日 int day = c.get(Calendar.DAY_OF_MONTH); //获取星期,由于老外们把星期日当作一周的第一天,所有星期日 返回对应的数字为1 String week = getWeek(c.get(Calendar.DAY_OF_WEEK)); System.out.println(year+"年"+mouth+"月"+day+"日"+week); } public static String getWeek(int i) { if(i<0 || i>7){ throw new RuntimeException(i+"没有对应的星期"); } String[] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; return weeks[i]; }}
4、日期练习
将毫秒值转成 指定的日期的格式 xxxx年xx月xx日 hh:mm:ss
//1,毫秒值time--->Date
Date date = new Date(time);
//2,通过日期时间格式器对象DateFormat对日期对象进行格式化(日期--->文本字符串)
DateFormat format = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);
String str_time = format.format(date);
System.out.println(str_time);
将"2014-9-19"转成日期对象。
之前有做过,将一个日期对象转成日期文本字符串这个称之为格式化。现在要做的是文本--->日期对象:解析。这些都是DateFormat中的功能。不同日期风格的文本对应
不同的格式器。
public static void main(String[] args) throws ParseException { String str_date = "2014年9月19日"; //日期格式器。DateFormat DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG); Date date = dateFormat.parse(str_date); System.out.println(date); //这种格式在给定的四种风格中不存在。怎么解析呢?只能使用子类SimpleDateFormat的方法, //1,构造函数明确特定风格,2,applyPattern 方法来修改格式模式 str_date = "2014/9/19 09--29--45"; DateFormat dateFormat2 = new SimpleDateFormat("yyyy/MM/dd hh--mm--ss"); Date date2 = dateFormat2.parse(str_date); System.out.println(date2); }
计算"2013-4-25"到"2013年7月29日"到底有多少天?
思路:
1,到底有多少天?相减的过程。
2,字符串也不能相减啊,毫秒值可以相减。
3,怎么获取毫秒值呢?毫秒值-->日期对象, 日期对象-->毫秒值。
4,怎么获取日期对象呢?需要将字符串文本--解析-->日期对象。
public class DateTest2 { public static void main(String[] args) throws ParseException { String str_date1 = "2013-4-25"; String str_date2 = "2013年7月29日"; //需要定义两个模式。一个解析str_date1,一个解析str_date2。 int style_1 = DateFormat.MEDIUM;//默认风格。 int style_2 = DateFormat.LONG;//默认风格。 int days = getDays(str_date1,str_date2, style_1,style_2); System.out.println("days="+days); } private static int getDays(String str_date1, String str_date2, int style_1, int style_2) throws ParseException { //1,根据给定风格创建格式器对象。 DateFormat format_1 = DateFormat.getDateInstance(style_1); DateFormat format_2 = DateFormat.getDateInstance(style_2); //2,对文本进行解析。 Date date_1 = format_1.parse(str_date1); Date date_2 = format_2.parse(str_date2); //3,获取日期对象毫秒值。 long time_1 = date_1.getTime(); long time_2 = date_2.getTime(); //4,相减。 long time = Math.abs(time_1 - time_2); int day = (int)(time/1000/60/60/24); return day; }}
5、获取给定年份的2月有多少天?
public class DateTest3 { public static void main(String[] args) { for (int year = 2000; year <= 2020; year++) { show(year); } } public static void show(int year) { Calendar c = Calendar.getInstance();// // 有获取有设置 set c.set(year, 2, 1); // 时间是连续的,3月1日的前一天就2月的最后一天,知道2月份的天数。 c.add(Calendar.DAY_OF_MONTH, -1); int year1 = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH) + 1; int day = c.get(Calendar.DAY_OF_MONTH); String week = getCnWeek(c.get(Calendar.DAY_OF_WEEK)); // 打印信息中年月日等相关信息都在这里。获取指定字段的值就哦了。 System.out.println(year1 + "年" + month + "月" + day + "日 " + week); } public static String getCnWeek(int i) { if (i < 0 || i > 7) { throw new RuntimeException(i + "没有对应的星期"); } // 定义表。 String[] weeks = { "", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; return weeks[i]; }}
==============================================
希望能与更多的java程序员共同学习
扣扣联系:2531977855
- 日期时间类
- ACE日期时间类
- 时间日期处理类
- 日期时间工具类
- 日期时间类
- 日期时间类
- 日期和时间类
- 日期时间类
- 日期时间工具类
- 日期时间类
- 日期时间类
- 日期时间类
- Unit12_problem4-日期时间类
- 日期时间类
- 日期时间类
- 日期时间类
- 时间日期类
- 日期时间帮助类
- Oracle入门理论
- 网络协议分层---入门一
- java之迷你浏览器
- dnw下载文件时can not open /dev/secbulk0的解决方法
- Android Service完全解析,关于服务你所需知道的一切
- 时间日期类
- 2014蓝桥杯C语言第六题 --回溯算法解决
- 网络协议分层--入门二
- OpenGL之路(八)添加光照效果和键盘控制
- 搜酷——全球智能硬件与技术精华集(全彩)
- ireport 启动错误cannot find java.exe
- 地址重定位:静态重定位和动态重定位
- servlet知识点,getId(),getValue()和getAttribute()区别?
- 怎样在Eclipse关联JDK源代码?