| | |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | import java.util.TimeZone; |
| | | |
| | | public class DateUtil { |
| | | |
| | | private static TimeZone tz = TimeZone.getTimeZone("GMT+0"); |
| | | /** |
| | | * 获取YYYY格式 |
| | | */ |
| | | public static String getYear() { |
| | | return formatDate(new Date(), "yyyy"); |
| | | } |
| | | public static Date getDate() { |
| | | TimeZone.setDefault(tz); |
| | | return new Date(); |
| | | } |
| | | public static Date getDate_str3(String dateStr) { |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | sdf.setTimeZone(tz); |
| | | if ("".equals(dateStr)) { |
| | | dateStr = sdf.format(DateUtil.getDate()); |
| | | } |
| | | Date date = null; |
| | | try { |
| | | date = sdf.parse(dateStr); |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return date; |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public static String getAfterDayWeek(String days) { |
| | | int daysInt = Integer.parseInt(days); |
| | | |
| | | Calendar canlendar = Calendar.getInstance(); // java.util包 |
| | | canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动 |
| | | Date date = canlendar.getTime(); |
| | |
| | | |
| | | return dateStr; |
| | | } |
| | | |
| | | |
| | | |
| | | public static int getNowWeekDay() { |
| | | Calendar canlendar = Calendar.getInstance(); // java.util包 |
| | | int week= canlendar.get(Calendar.DAY_OF_WEEK); |
| | | return week == 0 ? 7 : week - 1; |
| | | } |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | System.out.println(getAfterDayWeek("4")); |
| | | System.out.println(DateUtil.getNowWeekDay()); |
| | | } |
| | | } |