String startTimeStr = query.getTimeRange()[0]; String endTimeStr = query.getTimeRange()[1]; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date startDate = sdf.parse(startTimeStr); Date endDate = sdf.parse(endTimeStr); Calendar startCal = Calendar.getInstance(); startCal.setTime(startDate); startCal.set(Calendar.HOUR_OF_DAY, 0); startCal.set(Calendar.MINUTE, 0); startCal.set(Calendar.SECOND, 0); Calendar endCal = Calendar.getInstance(); endCal.setTime(endDate); endCal.set(Calendar.HOUR_OF_DAY, 23); endCal.set(Calendar.MINUTE, 59); endCal.set(Calendar.SECOND, 59); while (startCal.before(endCal)) { Date dayStart = startCal.getTime(); System.out.println("Day start: " + sdf.format(dayStart)); startCal.set(Calendar.HOUR_OF_DAY, 0); startCal.set(Calendar.MINUTE, 0); startCal.set(Calendar.SECOND, 0); Date dayEnd = startCal.getTime(); if (dayEnd.after(endCal.getTime())) { endCal.setTime(startCal.getTime()); endCal.set(Calendar.HOUR_OF_DAY, 23); endCal.set(Calendar.MINUTE, 59); endCal.set(Calendar.SECOND, 59); dayEnd = endCal.getTime(); } System.out.println("Day end: " + sdf.format(dayEnd)); startCal.add(Calendar.DAY_OF_MONTH, 1); } 用hutoo工具 java 优化
时间: 2024-01-24 08:05:14 浏览: 139
可以使用 Hutool 工具类中的 DateUtil 进行日期处理,简化代码:
```
String startTimeStr = query.getTimeRange()[0];
String endTimeStr = query.getTimeRange()[1];
Date startDate = DateUtil.parse(startTimeStr);
Date endDate = DateUtil.parse(endTimeStr);
while (startDate.before(endDate)) {
Date dayStart = DateUtil.beginOfDay(startDate);
System.out.println("Day start: " + DateUtil.formatDateTime(dayStart));
Date dayEnd = DateUtil.endOfDay(startDate);
System.out.println("Day end: " + DateUtil.formatDateTime(dayEnd));
startDate = DateUtil.offsetDay(startDate, 1);
}
```
相关问题
package com.music.zhangdaosen; import android.annotation.SuppressLint; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.MediaStore; import android.util.Log; import android.widget.ArrayAdapter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; public class SongHelper { public static List<Song>musicList = new ArrayList<>(); // static List<Song>musicList; public static List<Song> getLocalMusic(Context context){ Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; ContentResolver resolver = context.getContentResolver(); Cursor cursor = resolver.query(uri,null,null,null,null); if(cursor !=null){ Song song; while (cursor.moveToNext()){ song =new Song(); int index = cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME); song.name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)); song.singer = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)); song.path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); song.duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)); Log.d("SongHelper--keyar","get"+song.name+" "+song.duration); musicList.add(song); SimpleDateFormat sdf = new SimpleDateFormat("mm:ss"); String time = sdf.format(new Date(song.duration)); } Log.d("SongHelper--keyar","get musicList"+ musicList.size()); } cursor.close(); return musicList; } public static String changeTime(int duration){ int min = duration/1000/60; int sec = duration/1000%60; if(sec<10){ return min+":0"+sec; } return min+":"+sec; } }
这是一段 Java 代码,用于获取 Android 设备中的本地音乐列表。它使用了 Android 提供的 MediaStore API 来查询设备上的音乐媒体文件,然后将查询结果封装成一个 List<Song> 对象并返回。其中,每个 Song 对象表示一首歌曲,包含歌曲名称、歌手、路径、时长等信息。这段代码还包括一个 changeTime() 方法,用于将歌曲时长从毫秒转换为分钟和秒钟的格式。
if (bisTaskLike.getUpdateTime() != null) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date updateTime = sdf.parse(bisTaskLike.getUpdateTime()); queryWrapper.eq("update_time", updateTime); }报错Required type: String Provided: Date
这是因为你在使用 `queryWrapper.eq("update_time", updateTime)` 时,第二个参数的类型不正确。可以尝试将代码修改为:
```java
if (bisTaskLike.getUpdateTime() != null) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date updateTime = sdf.parse(bisTaskLike.getUpdateTime());
queryWrapper.eq("update_time", sdf.format(updateTime));
}
```
这里将 `updateTime` 格式化为字符串,并将其作为参数传入 `queryWrapper.eq()` 方法中。这样可以避免类型不匹配的问题。
阅读全文