TimeUtils.java 768 B

123456789101112131415161718192021222324252627282930
  1. package com.uas.report.util;
  2. import java.util.Calendar;
  3. import java.util.Date;
  4. /**
  5. * 对时间的操作
  6. *
  7. * @author sunyj
  8. * @since 2016年9月27日 下午8:10:50
  9. */
  10. public class TimeUtils {
  11. /**
  12. * Adds or subtracts the specified amount of time to the given calendar
  13. * field, based on the calendar's rules. For example, to subtract 5 days
  14. * from the specified time of the calendar, you can achieve it by calling:
  15. * add(time, Calendar.DAY_OF_MONTH, -5).
  16. *
  17. * @param time
  18. * @param field
  19. * @param amount
  20. * @return
  21. */
  22. public static Date add(Date time, int field, int amount) {
  23. Calendar calendar = Calendar.getInstance();
  24. calendar.setTime(time);
  25. calendar.add(field, amount);
  26. return calendar.getTime();
  27. }
  28. }