DayTest.java 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package com.service.test;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. public class DayTest {
  5. public static void main(String[] args) {
  6. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  7. String firstDay;
  8. String lastDay;
  9. //获取前月的第一天
  10. Calendar cal_1=Calendar.getInstance();//获取当前日期
  11. cal_1.add(Calendar.MONTH, -1);
  12. cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
  13. firstDay = format.format(cal_1.getTime());
  14. System.out.println("-----1------firstDay:"+firstDay+cal_1.getTime());
  15. //获取前月的最后一天
  16. Calendar cale = Calendar.getInstance();
  17. cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
  18. lastDay = format.format(cale.getTime());
  19. System.out.println("-----2------lastDay:"+lastDay);
  20. //获取当前月第一天:
  21. Calendar c = Calendar.getInstance();
  22. c.add(Calendar.MONTH, 0);
  23. c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
  24. String first = format.format(c.getTime());
  25. System.out.println("===============first:"+first);
  26. //获取当前月最后一天
  27. Calendar ca = Calendar.getInstance();
  28. ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
  29. String last = format.format(ca.getTime());
  30. System.out.println("===============last:"+last);
  31. // CallNativeApi api=new CallNativeApi();
  32. //api.
  33. }
  34. }