开始时间和结束时间设置为 yyyy-MM-dd 00:00:00 ~~yyyy-MM-dd 23:59:59

74 篇文章 0 订阅
        String format ="yyyy-MM-dd";
        String format1 ="yyyy-MM-dd 00:00:00";
        String format2 ="yyyy-MM-dd 23:59:59";
        
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        SimpleDateFormat sdf1 = new SimpleDateFormat(format1);
        SimpleDateFormat sdf2 = new SimpleDateFormat(format2);
        //将获取到的时间由String转换成Date形式
        Date StartTimeDate=sdf.parse(animalFromDTO.getStartTime());
        Date EndTimeDate=sdf.parse(animalFromDTO.getEndTime());
        //将时间转换成自己想要的格式
        String startTime = sdf1.format(StartTimeDate);
        String EndTime = sdf2.format(EndTimeDate);
        
        animalFromDTO.setStartTime(startTime);
        animalFromDTO.setEndTime(EndTime);
        
        System.out.println(startTime);
        System.out.println(EndTime);

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以通过以下代码获取某天的结束时间: ```java import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Demo { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); // 设置为当前时间 calendar.set(Calendar.HOUR_OF_DAY, 23); // 将小时数设置23 calendar.set(Calendar.MINUTE, 59); // 将分钟数设置59 calendar.set(Calendar.SECOND, 59); // 将秒数设置59 String endTime = sdf.format(calendar.getTime()); // 格式化输出 System.out.println(endTime); // 输出结果 } } ``` 其中,使用 `Calendar` 类设置小时数、分钟数和秒数,最后使用 `SimpleDateFormat` 类格式化输出,得到结束时间的字符串表示。 ### 回答2: 可以使用Java中的日期和时间类来获取某天的结束时间。具体步骤如下: 1. 首先,需要导入java.time包下的相关类,如下所示: ``` import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; ``` 2. 然后,使用`LocalDate`类的`now()`方法获取当前日期,使用`LocalTime`类的`MAX`常量获取一天的最后时间。再将日期和时间合并为完整的日期时间对象: ``` LocalDate currentDate = LocalDate.now(); LocalDateTime endTime = LocalDateTime.of(currentDate, LocalTime.MAX); ``` 3. 最后,使用`DateTimeFormatter`类来格式化日期时间对象为指定的格式: ``` DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String endTimeString = endTime.format(formatter); ``` 完整的代码如下: ``` import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { LocalDate currentDate = LocalDate.now(); LocalDateTime endTime = LocalDateTime.of(currentDate, LocalTime.MAX); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String endTimeString = endTime.format(formatter); System.out.println("结束时间:" + endTimeString); } } ``` 以上代码将输出当前日期的结束时间,格式为"yyyy-MM-dd HH:mm:ss"。 ### 回答3: 要获取某天的结束时间,可以使用Java中的`Calendar`和`SimpleDateFormat`类来实现。 首先,我们需要创建一个`Calendar`对象,并将日期设置为所需的某天。代码如下: ```java Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, 年); calendar.set(Calendar.MONTH, 月-1); calendar.set(Calendar.DAY_OF_MONTH, 日); ``` 以上的`年`、`月`和`日`分别代表所需日期的年份、月份和日期。需要注意的是,月份是从0开始计数的,所以如果要获取3月份的结束时间,需要将月份设置为2(即3-1)。 接下来,我们需要将时间设置为当天的最后一刻,即23:59:59。代码如下: ```java calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); ``` 然后,我们需要将时间格式化为指定的格式,即yyyy-MM-dd HH:mm:ss。代码如下: ```java SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String endTime = sdf.format(calendar.getTime()); ``` 最后,我们可以通过`endTime`变量获取到某天的结束时间,格式为yyyy-MM-dd HH:mm:ss。 完整的代码示例如下: ```java import java.util.Calendar; import java.text.SimpleDateFormat; public class GetEndTime { public static void main(String[] args) { int year = 2022; int month = 3; int day = 15; Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month-1); calendar.set(Calendar.DAY_OF_MONTH, day); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String endTime = sdf.format(calendar.getTime()); System.out.println("某天的结束时间为:" + endTime); } } ``` 以上代码可以得到某天的结束时间,输出为"某天的结束时间为:2022-03-15 23:59:59"。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值