java 获取当前天之后或之前7天日期

  1. /** 
  2.     * 获取过去或者未来 任意天内的日期数组 
  3.     * @param intervals      intervals天内 
  4.     * @return              日期数组 
  5.     */  
  6.    public static ArrayList<String> test(int intervals ) {  
  7.        ArrayList<String> pastDaysList = new ArrayList<>();  
  8.        ArrayList<String> fetureDaysList = new ArrayList<>();  
  9.        for (int i = 0; i <intervals; i++) {  
  10.            pastDaysList.add(getPastDate(i));  
  11.            fetureDaysList.add(getFetureDate(i));  
  12.        }  
  13.        return pastDaysList;  
  14.    }  
  15.   
  16.    /** 
  17.     * 获取过去第几天的日期 
  18.     * 
  19.     * @param past 
  20.     * @return 
  21.     */  
  22.    public static String getPastDate(int past) {  
  23.        Calendar calendar = Calendar.getInstance();  
  24.        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);  
  25.        Date today = calendar.getTime();  
  26.        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  27.        String result = format.format(today);  
  28.        Log.e(null, result);  
  29.        return result;  
  30.    }  
  31.   
  32.    /** 
  33.     * 获取未来 第 past 天的日期 
  34.     * @param past 
  35.     * @return 
  36.     */  
  37.    public static String getFetureDate(int past) {  
  38.        Calendar calendar = Calendar.getInstance();  
  39.        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);  
  40.        Date today = calendar.getTime();  
  41.        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  42.        String result = format.format(today);  
  43.        Log.e(null, result);  
  44.        return result;  
  45.    }  
Java中,你可以使用`java.util.Date`类和`java.text.SimpleDateFormat`类来获取日期中的月份和。以下是如何操作的步骤: 1. 创建一个`Date`对象,该对象默认会包含当前日期和时间的信息。 2. 使用`SimpleDateFormat`类来格式化日期,以便于提取月份和。 3. 使用`Calendar`类也可以获取月份和,但这是一个较老的方法,现在更推荐使用`java.time`包中的类,比如`LocalDate`。 以下是使用`SimpleDateFormat`和`Date`类的示例代码: ```java import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) { try { // 创建一个Date对象,它默认包含当前日期和时间 Date date = new Date(); // 创建一个SimpleDateFormat对象,用于定义日期格式 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 使用format方法来格式化日期 String formattedDate = dateFormat.format(date); System.out.println("当前日期和时间:" + formattedDate); // 从格式化的字符串中提取月份和 int month = Integer.parseInt(formattedDate.substring(5, 7)); int day = Integer.parseInt(formattedDate.substring(8, 10)); System.out.println("当前月份:" + (month)); System.out.println("当前:" + day); } catch (Exception e) { e.printStackTrace(); } } } ``` 请注意,在上述代码中,月份是从0开始计数的,即1月是0,2月是1,以此类推。因此,如果你想获取正确的月份,可能需要在获取的月份值上加1。 此外,`java.time`包提供了更为强大和灵活的日期时间API,自Java 8起成为首选。以下是使用`java.time.LocalDate`类获取月份和的示例代码: ```java import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { // 获取当前日期 LocalDate localDate = LocalDate.now(); // 获取月份和 int month = localDate.getMonthValue(); // 月份,例如:1代表1月 int day = localDate.getDayOfMonth(); // 月份中的,例如:3代表3号 System.out.println("当前月份:" + month); System.out.println("当前:" + day); } } ``` `java.time.LocalDate`类会自动处理月份从1开始的问题,因此你可以直接使用`getMonthValue()`方法获取月份。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值