java 获取当月第一天和最后一天
时间: 2024-01-30 14:02:05 浏览: 201
获取当月第一天的Java代码:
```java
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1); // 设置为当月第一天
Date firstDayOfMonth = calendar.getTime();
```
获取当月最后一天的Java代码:
```java
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 设置为当月最后一天
Date lastDayOfMonth = calendar.getTime();
```
或者使用Java 8的新日期时间API:
```java
LocalDate now = LocalDate.now();
LocalDate firstDayOfMonth = now.withDayOfMonth(1);
LocalDate lastDayOfMonth = now.withDayOfMonth(now.lengthOfMonth());
```
相关问题
java获取当月第一天和最后一天,当年第一天和最后一天,上个月第一天和最后一天,去年第一天和最后一天,并用Long类型时间戳表示结果
要获取Java中的日期信息,可以使用`java.util.Calendar`类或者`java.time.LocalDate`类。下面是使用`java.util.Calendar`类的示例代码来获取所需的日期信息,并将结果转换为Long类型的时间戳表示:
```java
import java.util.Calendar;
public class DateUtils {
public static void main(String[] args) {
// 获取当前日期
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1; // 月份从0开始,需要加1
int day = calendar.get(Calendar.DAY_OF_MONTH);
// 获取当月第一天和最后一天
calendar.set(Calendar.DAY_OF_MONTH, 1);
long firstDayOfMonth = calendar.getTimeInMillis();
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
long lastDayOfMonth = calendar.getTimeInMillis();
// 获取当年第一天和最后一天
calendar.set(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
long firstDayOfYear = calendar.getTimeInMillis();
calendar.set(Calendar.MONTH, 11);
calendar.set(Calendar.DAY_OF_MONTH, 31);
long lastDayOfYear = calendar.getTimeInMillis();
// 获取上个月第一天和最后一天
if (month == 1) {
calendar.set(Calendar.YEAR, year - 1);
calendar.set(Calendar.MONTH, 11);
} else {
calendar.set(Calendar.MONTH, month - 2);
}
calendar.set(Calendar.DAY_OF_MONTH, 1);
long firstDayOfLastMonth = calendar.getTimeInMillis();
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
long lastDayOfLastMonth = calendar.getTimeInMillis();
// 获取去年第一天和最后一天
calendar.set(Calendar.YEAR, year - 1);
calendar.set(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
long firstDayOfLastYear = calendar.getTimeInMillis();
calendar.set(Calendar.MONTH, 11);
calendar.set(Calendar.DAY_OF_MONTH, 31);
long lastDayOfLastYear = calendar.getTimeInMillis();
// 输出结果
System.out.println("当月第一天:" + firstDayOfMonth);
System.out.println("当月最后一天:" + lastDayOfMonth);
System.out.println("当年第一天:" + firstDayOfYear);
System.out.println("当年最后一天:" + lastDayOfYear);
System.out.println("上个月第一天:" + firstDayOfLastMonth);
System.out.println("上个月最后一天:" + lastDayOfLastMonth);
System.out.println("去年第一天:" + firstDayOfLastYear);
System.out.println("去年最后一天:" + lastDayOfLastYear);
}
}
```
Java获取当月的第一天和最后一天日期
可以使用Java中的Calendar类来获取当月的第一天和最后一天日期。具体实现如下:
```
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
// 获取当前时间
Calendar calendar = Calendar.getInstance();
// 设置为当月第一天
calendar.set(Calendar.DAY_OF_MONTH, 1);
// 获取当月第一天的日期
String firstDayOfMonth = String.format("%tF", calendar);
System.out.println("当月第一天日期:" + firstDayOfMonth);
// 设置为当月最后一天
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
// 获取当月最后一天的日期
String lastDayOfMonth = String.format("%tF", calendar);
System.out.println("当月最后一天日期:" + lastDayOfMonth);
}
}
```
输出结果如下:
```
当月第一天日期:2022-02-01
当月最后一天日期:2022-02-28
```
阅读全文