| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取当前月第一天 |
| | | * @return |
| | | */ |
| | | public static String getFirstDayOfMonthString() { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | |
| | | calendar.add(Calendar.MONTH, 0); |
| | | |
| | | calendar.set(Calendar.DAY_OF_MONTH, 1); |
| | | // 格式化日期 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | return sdf.format(calendar.getTime())+" 00:00:00"; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前月最后一天 |
| | | * @return |
| | | */ |
| | | public static String getLastDayOfMonthString() { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | |
| | | calendar.add(Calendar.MONTH, 1); |
| | | |
| | | calendar.set(Calendar.DAY_OF_MONTH, 0); |
| | | // 格式化日期 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | return sdf.format(calendar.getTime())+" 23:59:59"; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前时间(年月日) |
| | | * @return |
| | | */ |
| | | public static String getDayOfMonthString() { |
| | | Calendar today = Calendar.getInstance(); |
| | | today.set(Calendar.HOUR, 0); |
| | | today.set(Calendar.MINUTE, 0); |
| | | today.set(Calendar.SECOND, 0); |
| | | today.set(Calendar.MILLISECOND, 0); |
| | | // 格式化日期 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | return sdf.format(today.getTime()); |
| | | } |
| | | |
| | | /** |
| | | * 日期转周 |
| | | * |
| | | * @param datetime |
| | | * @return |
| | | */ |
| | | public static String dateToWeek(String datetime) { |
| | | SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd"); |
| | | String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" }; |
| | | Calendar cal = Calendar.getInstance(); // 获得一个日历 |
| | | Date datet = null; |
| | | try { |
| | | if (StringUtils.isNotEmpty(datetime)) { |
| | | datet = f.parse(datetime); |
| | | cal.setTime(datet); |
| | | } |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个周中的某天。 |
| | | if (w < 0) |
| | | w = 0; |
| | | return weekDays[w]; |
| | | } |
| | | |
| | | |
| | | |
| | | public static void main(String[]args)throws Exception{ |
| | | // Date date= new Date(); |
| | | // Date after = new Date(); |
| | | // System.out.println(calTimeDifference(date,after)); |
| | | // System.out.println(getFirstDayOfMonthString()); |
| | | // System.out.println(getDayOfMonthString()); |
| | | |
| | | } |
| | | } |