赞
踩
Java获取当前年份、月份和日期是通过Calendar类的实例对象来获取的。
首先创建一个Calendar类的实例对象,Calendar类属于java.util包。
Calendar calendar = Calendar.getInstance();
获取当前年份、月份和日期等。
- // 获取当前年
- int year = calendar.get(Calendar.YEAR);
- // 获取当前月
- int month = calendar.get(Calendar.MONTH) + 1;
- // 获取当前日
- int day = calendar.get(Calendar.DATE);
- // 获取当前小时
- int hour = calendar.get(Calendar.HOUR_OF_DAY);
- // 获取当前分钟
- int minute = calendar.get(Calendar.MINUTE);
- // 获取当前秒
- int second = calendar.get(Calendar.SECOND);
- // 获取当前是本周第几天
- int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
- // 获取当前是本月第几天
- int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
- // 获取当前是本年第几天
- int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
获取当月的第一天和最后一天的字符串。
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- // 获取当月第一天
- calendar = Calendar.getInstance();
- calendar.add(Calendar.MONTH, 0);
- calendar.set(Calendar.DAY_OF_MONTH, 1);
- String firstday = format.format(calendar.getTime());
- // 获取当月最后一天
- calendar = Calendar.getInstance();
- calendar.add(Calendar.MONTH, 1);
- calendar.set(Calendar.DAY_OF_MONTH, 0);
- String lastday = format.format(calendar.getTime());
- // 打印结果字符串
- System.out.println("本月第一天和最后一天分别是:" + firstday + " 和 " + lastday + "。");
另外也可以使用Date类的实例对象配合SimpleDateFormat类的实例对象来获取当前日期字符串。
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- Date date = new Date();
- System.out.println("当前日期字符串:" + format.format(date) + "。");
获取上个星期第一天的某个小时的时间戳
- public static Long getPreviousWeekday(int hour) {
- int week = -1;
- int mondayPlus = getMondayPlus();
- GregorianCalendar currentDate = new GregorianCalendar();
- currentDate.add(Calendar.DATE, mondayPlus + 7 * week);
- Date monday = currentDate.getTime();
- TimeZone curTimeZone = TimeZone.getTimeZone("GMT+8");
- Calendar c = Calendar.getInstance(curTimeZone);
- c.setTime(monday);
- c.set(Calendar.HOUR_OF_DAY, hour);
- c.set(Calendar.MINUTE, 0);
- c.set(Calendar.SECOND, 0);
- // Date z = c.getTime();
- // SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd);
- // System.out.println(sdf.format(z));
- return c.getTime().getTime();
- }
-
-
-
- public static int getMondayPlus() {
- Calendar cd = Calendar.getInstance();
- int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1;
- if (dayOfWeek == 1) {
- return 0;
- }
- return (1 - dayOfWeek);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。