java 本月 第一天 至 第七天 用 Calendar实现
时间: 2024-04-19 15:04:07 浏览: 9
以下是实现java本月第一天至第七天的代码,使用了Calendar类:
```
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
int month = cal.get(Calendar.MONTH) + 1; // 获取当前月份(注意:月份从0开始计数)
// 输出本月第一天
System.out.println("本月第一天:" + month + "-" + cal.get(Calendar.DAY_OF_MONTH));
// 输出本月第二天至第七天
for (int i = 2; i <= 7; i++) {
cal.add(Calendar.DAY_OF_MONTH, 1);
System.out.println("本月第" + i + "天:" + month + "-" + cal.get(Calendar.DAY_OF_MONTH));
}
}
}
```
运行结果如下:
```
本月第一天:8-1
本月第2天:8-2
本月第3天:8-3
本月第4天:8-4
本月第5天:8-5
本月第6天:8-6
本月第7天:8-7
```
相关问题
使用Java编程和springboot和mybatis框架,连接mysql数据库,给出具体程序,包含Controller层、Service层、Service实现类、Dao层以及Mapper.xml文件,统计本周第一天 本月第一天 本年第一天起到今天为止的中奖趋势接口 并对接口使用redis进行缓存处理 过期时间10分钟
首先,需要添加相关依赖:
```xml
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Starter Data JDBC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
```
接下来,创建相关类:
1. Trend.java
```java
public class Trend {
private String date;
private int count;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
```
2. TrendMapper.xml
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.lottery.mapper.TrendMapper">
<resultMap id="TrendMap" type="com.example.lottery.entity.Trend">
<result column="date" property="date" jdbcType="VARCHAR" />
<result column="count" property="count" jdbcType="INTEGER" />
</resultMap>
<select id="selectTrend" resultMap="TrendMap">
SELECT DATE_FORMAT(lottery_time, '%Y-%m-%d') as date, COUNT(*) as count
FROM lottery
WHERE lottery_time BETWEEN #{weekBegin} AND NOW()
GROUP BY DATE_FORMAT(lottery_time, '%Y-%m-%d')
ORDER BY DATE_FORMAT(lottery_time, '%Y-%m-%d') DESC
</select>
</mapper>
```
3. TrendMapper.java
```java
@Mapper
public interface TrendMapper {
List<Trend> selectTrend(@Param("weekBegin") Date weekBegin);
}
```
4. TrendService.java
```java
public interface TrendService {
List<Trend> getTrend();
}
```
5. TrendServiceImpl.java
```java
@Service
public class TrendServiceImpl implements TrendService {
@Autowired
private TrendMapper trendMapper;
@Autowired
private RedisTemplate<String, List<Trend>> redisTemplate;
@Override
public List<Trend> getTrend() {
String key = "trend";
List<Trend> trendList = redisTemplate.opsForValue().get(key);
if (trendList != null) {
return trendList;
}
Date weekBegin = getWeekBegin();
trendList = trendMapper.selectTrend(weekBegin);
redisTemplate.opsForValue().set(key, trendList, Duration.ofMinutes(10));
return trendList;
}
private Date getWeekBegin() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
}
```
6. TrendController.java
```java
@RestController
@RequestMapping("/trend")
public class TrendController {
@Autowired
private TrendService trendService;
@GetMapping
public List<Trend> getTrend() {
return trendService.getTrend();
}
}
```
7. RedisConfig.java
```java
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, List<Trend>> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, List<Trend>> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(List.class));
return redisTemplate;
}
}
```
最后,修改application.properties或application.yml配置文件,添加数据库和Redis相关配置。
注意,以上代码仅供参考,具体实现可能需要根据实际情况进行调整。
用java写一个控制台输入年月日,打印出本月日历
好的,以下是Java代码实现:
```java
import java.util.*;
public class PrintCalendar {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入日期(格式:2022-01):");
String str = input.nextLine(); // 例如输入:2022-01
String[] arr = str.split("-");
int year = Integer.parseInt(arr[0]);
int month = Integer.parseInt(arr[1]);
input.close();
// 获取当月的天数和第一天是星期几
int days = getDaysOfMonth(year, month);
int firstDayOfWeek = getFirstDayOfWeek(year, month);
// 输出日历
System.out.println("日\t一\t二\t三\t四\t五\t六");
for (int i = 1; i <= days; i++) {
if (i == 1) {
for (int j = 0; j < firstDayOfWeek; j++) {
System.out.print("\t");
}
}
System.out.print(i + "\t");
if ((i + firstDayOfWeek) % 7 == 0) {
System.out.println();
}
}
}
// 获取当月的天数
public static int getDaysOfMonth(int year, int month) {
int days = 0;
if (month == 2) {
days = year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ? 29 : 28;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
} else {
days = 31;
}
return days;
}
// 获取当月第一天是星期几(星期日为0,星期一为1,...,星期六为6)
public static int getFirstDayOfWeek(int year, int month) {
Calendar c = Calendar.getInstance();
c.set(year, month - 1, 1);
return c.get(Calendar.DAY_OF_WEEK) - 1; // 对应上面的0~6
}
}
```
使用示例:
```
请输入日期(格式:2022-01):2022-01
日 一 二 三 四 五 六
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
```