使用 "hh:mm" Java
Operating with "hh:mm" Java
我有一个 table 的小时和分钟范围,“07:00”到“14:00”,还有一个 Java Api 获取这些值。
起初我使用的是秒(28800 到 50400)。
我想每 5 分钟获取一次中间的所有值(“07:05”、“07:10”、“07:15”、“07:20”...“08:15” ...“13:55”、“14:00”)。
但我不太清楚如何以干净的方式做到这一点。
Class 方法(在控制台上效果很好,但 api 的输出以纳秒为单位:
@Transient
public List<LocalTime> getHorarios(){
long initialCapacity = 0;
List<LocalTime> times = new ArrayList<LocalTime> ( ( int ) initialCapacity );
while ( ! nextTime.isAfter ( stop ) ) {
times.add ( nextTime );
nextTime = nextTime.plusMinutes ( 5 );
}
return times;
}
@Transient
public List<LocalTime> getHorariosLibres(List<LocalTime> horariosTomados){
List<LocalTime> horariosLibres = getHorarios();
horariosLibres.removeAll(horariosTomados);
return horariosLibres;
}
到目前为止,这是我的控制器:
@RequestMapping(value = "/horario/{fecha}", method = RequestMethod.GET)
@ResponseBody
public Object queryHorariosLibres(@PathVariable("fecha") @DateTimeFormat(pattern="yyyy-MM-dd") Date fecha) {
List<LocalTime> horariosTomados = turnoService.getHorariosTomados(fecha);
Calendar dia = new GregorianCalendar();
dia.setTime(fecha);
Horario horario = horarioRepository.findByDia(dia.get(Calendar.DAY_OF_WEEK));
List<LocalTime> horariosLibres = horario.getHorariosLibres(horariosTomados);
return horariosLibres;
}
通过API的实际输出:
[{"hour":7,"minute":0,"second":0,"nano":0},{"hour":7,"minute":5,"second":0,"nano":0},{"hour":7,"minute":10,"second":0,"nano":0},{"hour":7,"minute":15,"second":0,"nano":0},{"hour":7,"minute":20,"second":0,"nano":0},{"hour":7,"minute":25,"second":0,"nano":0},{"hour":7,"minute":30,"second":0,"nano":0},{"hour":7,"minute":35,"second":0,"nano":0},{"hour":7,"minute":40,"second":0,"nano":0},{"hour":7,"minute":45,"second":0,"nano":0},{"hour":7,"minute":50,"second":0,"nano":0},{"hour":7,"minute":55,"second":0,"nano":0},{"hour":8,"minute":0,"second":0,"nano":0},{"hour":8,"minute":5,"second":0,"nano":0},{"hour":8,"minute":10,"second":0,"nano":0},{"hour":8,"minute":15,"second":0,"nano":0},{"hour":8,"minute":20,"second":0,"nano":0},{"hour":8,"minute":25,"second":0,"nano":0},{"hour":8,"minute":30,"second":0,"nano":0},{"hour":8,"minute":35,"second":0,"nano":0},{"hour":8,"minute":40,"second":0,"nano":0},{"hour":8,"minute":45,"second":0,"nano":0},{"hour":8,"minute":50,"second":0,"nano":0},{"hour":8,"minute":55,"second":0,"nano":0},{"hour":9,"minute":0,"second":0,"nano":0},{"hour":9,"minute":5,"second":0,"nano":0},{"hour":9,"minute":10,"second":0,"nano":0},{"hour":9,"minute":15,"second":0,"nano":0},{"hour":9,"minute":20,"second":0,"nano":0},{"hour":9,"minute":25,"second":0,"nano":0},{"hour":9,"minute":30,"second":0,"nano":0},{"hour":9,"minute":35,"second":0,"nano":0},{"hour":9,"minute":40,"second":0,"nano":0},{"hour":9,"minute":45,"second":0,"nano":0},{"hour":9,"minute":50,"second":0,"nano":0},{"hour":9,"minute":55,"second":0,"nano":0},{"hour":10,"minute":0,"second":0,"nano":0},{"hour":10,"minute":5,"second":0,"nano":0},{"hour":10,"minute":10,"second":0,"nano":0},{"hour":10,"minute":15,"second":0,"nano":0},{"hour":10,"minute":20,"second":0,"nano":0},{"hour":10,"minute":25,"second":0,"nano":0},{"hour":10,"minute":30,"second":0,"nano":0},{"hour":10,"minute":35,"second":0,"nano":0},{"hour":10,"minute":40,"second":0,"nano":0},{"hour":10,"minute":45,"second":0,"nano":0},{"hour":10,"minute":50,"second":0,"nano":0},{"hour":10,"minute":55,"second":0,"nano":0},{"hour":11,"minute":0,"second":0,"nano":0},{"hour":11,"minute":5,"second":0,"nano":0},{"hour":11,"minute":10,"second":0,"nano":0},{"hour":11,"minute":15,"second":0,"nano":0},{"hour":11,"minute":20,"second":0,"nano":0},{"hour":11,"minute":25,"second":0,"nano":0},{"hour":11,"minute":30,"second":0,"nano":0},{"hour":11,"minute":35,"second":0,"nano":0},{"hour":11,"minute":40,"second":0,"nano":0},{"hour":11,"minute":45,"second":0,"nano":0},{"hour":11,"minute":50,"second":0,"nano":0},{"hour":11,"minute":55,"second":0,"nano":0},{"hour":12,"minute":0,"second":0,"nano":0},{"hour":12,"minute":5,"second":0,"nano":0},{"hour":12,"minute":10,"second":0,"nano":0},{"hour":12,"minute":15,"second":0,"nano":0},{"hour":12,"minute":20,"second":0,"nano":0},{"hour":12,"minute":25,"second":0,"nano":0},{"hour":12,"minute":30,"second":0,"nano":0},{"hour":12,"minute":35,"second":0,"nano":0},{"hour":12,"minute":40,"second":0,"nano":0},{"hour":12,"minute":45,"second":0,"nano":0},{"hour":12,"minute":50,"second":0,"nano":0},{"hour":12,"minute":55,"second":0,"nano":0},{"hour":13,"minute":0,"second":0,"nano":0},{"hour":13,"minute":5,"second":0,"nano":0},{"hour":13,"minute":10,"second":0,"nano":0},{"hour":13,"minute":15,"second":0,"nano":0},{"hour":13,"minute":20,"second":0,"nano":0},{"hour":13,"minute":25,"second":0,"nano":0},{"hour":13,"minute":30,"second":0,"nano":0},{"hour":13,"minute":35,"second":0,"nano":0},{"hour":13,"minute":40,"second":0,"nano":0},{"hour":13,"minute":45,"second":0,"nano":0},{"hour":13,"minute":50,"second":0,"nano":0},{"hour":13,"minute":55,"second":0,"nano":0},{"hour":14,"minute":0,"second":0,"nano":0}]
我会以分钟为单位并使用 % 和 /。像
Map<String,Integer> storage = new HashMap<String,Integer>();
for (int i=420; i< 840; i+=5)
{
int hours = (i / 60);
int minutes = (i % 60);
String hourMinuteString = String.format("%02d", hours) + ":"
+ String.format("%02d", minutes);
storage.put(hourMinuteString, i);
}
然后您可以通过
检索分钟数
storage.get("10:45");
无需在此处滚动您自己的数据类型。
java.time.LocalTime
Java 8 及更高版本有适合这种情况的数据类型:java.time.LocalTime
,没有日期和时区的时间。将其用于您的业务逻辑。从这些对象中,您可以生成一个字符串表示形式以显示给用户。
java.sql.Time
JDBC 有一种数据类型用于将此类值传入和传出数据库:java.sql.Time
.
String inputStart = "07:00";
String inputStop = "14:00";
LocalTime start = LocalTime.parse ( inputStart );
LocalTime stop = LocalTime.parse ( inputStop );
LocalTime nextTime = start;
Duration duration = Duration.ofMinutes ( 5 );
long initialCapacity = ( Duration.between ( start , stop ).toMinutes () / duration.toMinutes () ) + 1; // Optional line of code. Could be omitted.
List<LocalTime> times = new ArrayList ( ( int ) initialCapacity );
while ( ! nextTime.isAfter ( stop ) ) {
times.add ( nextTime );
nextTime = nextTime.plus ( duration ); // Or call .plusMinutes( int ) and pass a number of minutes.
}
转储到控制台。
System.out.println ( "times: " + times + " initialCapacity: " + initialCapacity + " times.size " + times.size () );
times: [07:00, 07:05, 07:10, 07:15, 07:20, 07:25, 07:30, 07:35, 07:40, 07:45, 07:50, 07:55, 08:00, 08:05, 08:10, 08:15, 08:20, 08:25, 08:30, 08:35, 08:40, 08:45, 08:50, 08:55, 09:00, 09:05, 09:10, 09:15, 09:20, 09:25, 09:30, 09:35, 09:40, 09:45, 09:50, 09:55, 10:00, 10:05, 10:10, 10:15, 10:20, 10:25, 10:30, 10:35, 10:40, 10:45, 10:50, 10:55, 11:00, 11:05, 11:10, 11:15, 11:20, 11:25, 11:30, 11:35, 11:40, 11:45, 11:50, 11:55, 12:00, 12:05, 12:10, 12:15, 12:20, 12:25, 12:30, 12:35, 12:40, 12:45, 12:50, 12:55, 13:00, 13:05, 13:10, 13:15, 13:20, 13:25, 13:30, 13:35, 13:40, 13:45, 13:50, 13:55, 14:00] initialCapacity: 85 times.size 85
要输入数据库,请转换为 java.sql.Time
。
java.sql.Time sqlTime = java.sql.Time.valueOf( myLocalTime );
时区
重要提示:请注意,您的原始代码和此 java.time 代码示例仅使用假定 的假想时间虚构的 24 小时通用日 。夏令时 (DST) 等异常意味着日子并不总是 24 小时。您列出的某些时间可能不存在于特定时区的特定日期。
如果您关心真实时刻、特定日期的有效时间,请使用 java.time.ZonedDateTime
(或 .OffsetDateTime
)。对于数据库传输,使用 java.sql.Timestamp
.
我有一个 table 的小时和分钟范围,“07:00”到“14:00”,还有一个 Java Api 获取这些值。 起初我使用的是秒(28800 到 50400)。
我想每 5 分钟获取一次中间的所有值(“07:05”、“07:10”、“07:15”、“07:20”...“08:15” ...“13:55”、“14:00”)。
但我不太清楚如何以干净的方式做到这一点。
Class 方法(在控制台上效果很好,但 api 的输出以纳秒为单位:
@Transient
public List<LocalTime> getHorarios(){
long initialCapacity = 0;
List<LocalTime> times = new ArrayList<LocalTime> ( ( int ) initialCapacity );
while ( ! nextTime.isAfter ( stop ) ) {
times.add ( nextTime );
nextTime = nextTime.plusMinutes ( 5 );
}
return times;
}
@Transient
public List<LocalTime> getHorariosLibres(List<LocalTime> horariosTomados){
List<LocalTime> horariosLibres = getHorarios();
horariosLibres.removeAll(horariosTomados);
return horariosLibres;
}
到目前为止,这是我的控制器:
@RequestMapping(value = "/horario/{fecha}", method = RequestMethod.GET)
@ResponseBody
public Object queryHorariosLibres(@PathVariable("fecha") @DateTimeFormat(pattern="yyyy-MM-dd") Date fecha) {
List<LocalTime> horariosTomados = turnoService.getHorariosTomados(fecha);
Calendar dia = new GregorianCalendar();
dia.setTime(fecha);
Horario horario = horarioRepository.findByDia(dia.get(Calendar.DAY_OF_WEEK));
List<LocalTime> horariosLibres = horario.getHorariosLibres(horariosTomados);
return horariosLibres;
}
通过API的实际输出:
[{"hour":7,"minute":0,"second":0,"nano":0},{"hour":7,"minute":5,"second":0,"nano":0},{"hour":7,"minute":10,"second":0,"nano":0},{"hour":7,"minute":15,"second":0,"nano":0},{"hour":7,"minute":20,"second":0,"nano":0},{"hour":7,"minute":25,"second":0,"nano":0},{"hour":7,"minute":30,"second":0,"nano":0},{"hour":7,"minute":35,"second":0,"nano":0},{"hour":7,"minute":40,"second":0,"nano":0},{"hour":7,"minute":45,"second":0,"nano":0},{"hour":7,"minute":50,"second":0,"nano":0},{"hour":7,"minute":55,"second":0,"nano":0},{"hour":8,"minute":0,"second":0,"nano":0},{"hour":8,"minute":5,"second":0,"nano":0},{"hour":8,"minute":10,"second":0,"nano":0},{"hour":8,"minute":15,"second":0,"nano":0},{"hour":8,"minute":20,"second":0,"nano":0},{"hour":8,"minute":25,"second":0,"nano":0},{"hour":8,"minute":30,"second":0,"nano":0},{"hour":8,"minute":35,"second":0,"nano":0},{"hour":8,"minute":40,"second":0,"nano":0},{"hour":8,"minute":45,"second":0,"nano":0},{"hour":8,"minute":50,"second":0,"nano":0},{"hour":8,"minute":55,"second":0,"nano":0},{"hour":9,"minute":0,"second":0,"nano":0},{"hour":9,"minute":5,"second":0,"nano":0},{"hour":9,"minute":10,"second":0,"nano":0},{"hour":9,"minute":15,"second":0,"nano":0},{"hour":9,"minute":20,"second":0,"nano":0},{"hour":9,"minute":25,"second":0,"nano":0},{"hour":9,"minute":30,"second":0,"nano":0},{"hour":9,"minute":35,"second":0,"nano":0},{"hour":9,"minute":40,"second":0,"nano":0},{"hour":9,"minute":45,"second":0,"nano":0},{"hour":9,"minute":50,"second":0,"nano":0},{"hour":9,"minute":55,"second":0,"nano":0},{"hour":10,"minute":0,"second":0,"nano":0},{"hour":10,"minute":5,"second":0,"nano":0},{"hour":10,"minute":10,"second":0,"nano":0},{"hour":10,"minute":15,"second":0,"nano":0},{"hour":10,"minute":20,"second":0,"nano":0},{"hour":10,"minute":25,"second":0,"nano":0},{"hour":10,"minute":30,"second":0,"nano":0},{"hour":10,"minute":35,"second":0,"nano":0},{"hour":10,"minute":40,"second":0,"nano":0},{"hour":10,"minute":45,"second":0,"nano":0},{"hour":10,"minute":50,"second":0,"nano":0},{"hour":10,"minute":55,"second":0,"nano":0},{"hour":11,"minute":0,"second":0,"nano":0},{"hour":11,"minute":5,"second":0,"nano":0},{"hour":11,"minute":10,"second":0,"nano":0},{"hour":11,"minute":15,"second":0,"nano":0},{"hour":11,"minute":20,"second":0,"nano":0},{"hour":11,"minute":25,"second":0,"nano":0},{"hour":11,"minute":30,"second":0,"nano":0},{"hour":11,"minute":35,"second":0,"nano":0},{"hour":11,"minute":40,"second":0,"nano":0},{"hour":11,"minute":45,"second":0,"nano":0},{"hour":11,"minute":50,"second":0,"nano":0},{"hour":11,"minute":55,"second":0,"nano":0},{"hour":12,"minute":0,"second":0,"nano":0},{"hour":12,"minute":5,"second":0,"nano":0},{"hour":12,"minute":10,"second":0,"nano":0},{"hour":12,"minute":15,"second":0,"nano":0},{"hour":12,"minute":20,"second":0,"nano":0},{"hour":12,"minute":25,"second":0,"nano":0},{"hour":12,"minute":30,"second":0,"nano":0},{"hour":12,"minute":35,"second":0,"nano":0},{"hour":12,"minute":40,"second":0,"nano":0},{"hour":12,"minute":45,"second":0,"nano":0},{"hour":12,"minute":50,"second":0,"nano":0},{"hour":12,"minute":55,"second":0,"nano":0},{"hour":13,"minute":0,"second":0,"nano":0},{"hour":13,"minute":5,"second":0,"nano":0},{"hour":13,"minute":10,"second":0,"nano":0},{"hour":13,"minute":15,"second":0,"nano":0},{"hour":13,"minute":20,"second":0,"nano":0},{"hour":13,"minute":25,"second":0,"nano":0},{"hour":13,"minute":30,"second":0,"nano":0},{"hour":13,"minute":35,"second":0,"nano":0},{"hour":13,"minute":40,"second":0,"nano":0},{"hour":13,"minute":45,"second":0,"nano":0},{"hour":13,"minute":50,"second":0,"nano":0},{"hour":13,"minute":55,"second":0,"nano":0},{"hour":14,"minute":0,"second":0,"nano":0}]
我会以分钟为单位并使用 % 和 /。像
Map<String,Integer> storage = new HashMap<String,Integer>();
for (int i=420; i< 840; i+=5)
{
int hours = (i / 60);
int minutes = (i % 60);
String hourMinuteString = String.format("%02d", hours) + ":"
+ String.format("%02d", minutes);
storage.put(hourMinuteString, i);
}
然后您可以通过
检索分钟数 storage.get("10:45");
无需在此处滚动您自己的数据类型。
java.time.LocalTime
Java 8 及更高版本有适合这种情况的数据类型:java.time.LocalTime
,没有日期和时区的时间。将其用于您的业务逻辑。从这些对象中,您可以生成一个字符串表示形式以显示给用户。
java.sql.Time
JDBC 有一种数据类型用于将此类值传入和传出数据库:java.sql.Time
.
String inputStart = "07:00";
String inputStop = "14:00";
LocalTime start = LocalTime.parse ( inputStart );
LocalTime stop = LocalTime.parse ( inputStop );
LocalTime nextTime = start;
Duration duration = Duration.ofMinutes ( 5 );
long initialCapacity = ( Duration.between ( start , stop ).toMinutes () / duration.toMinutes () ) + 1; // Optional line of code. Could be omitted.
List<LocalTime> times = new ArrayList ( ( int ) initialCapacity );
while ( ! nextTime.isAfter ( stop ) ) {
times.add ( nextTime );
nextTime = nextTime.plus ( duration ); // Or call .plusMinutes( int ) and pass a number of minutes.
}
转储到控制台。
System.out.println ( "times: " + times + " initialCapacity: " + initialCapacity + " times.size " + times.size () );
times: [07:00, 07:05, 07:10, 07:15, 07:20, 07:25, 07:30, 07:35, 07:40, 07:45, 07:50, 07:55, 08:00, 08:05, 08:10, 08:15, 08:20, 08:25, 08:30, 08:35, 08:40, 08:45, 08:50, 08:55, 09:00, 09:05, 09:10, 09:15, 09:20, 09:25, 09:30, 09:35, 09:40, 09:45, 09:50, 09:55, 10:00, 10:05, 10:10, 10:15, 10:20, 10:25, 10:30, 10:35, 10:40, 10:45, 10:50, 10:55, 11:00, 11:05, 11:10, 11:15, 11:20, 11:25, 11:30, 11:35, 11:40, 11:45, 11:50, 11:55, 12:00, 12:05, 12:10, 12:15, 12:20, 12:25, 12:30, 12:35, 12:40, 12:45, 12:50, 12:55, 13:00, 13:05, 13:10, 13:15, 13:20, 13:25, 13:30, 13:35, 13:40, 13:45, 13:50, 13:55, 14:00] initialCapacity: 85 times.size 85
要输入数据库,请转换为 java.sql.Time
。
java.sql.Time sqlTime = java.sql.Time.valueOf( myLocalTime );
时区
重要提示:请注意,您的原始代码和此 java.time 代码示例仅使用假定 的假想时间虚构的 24 小时通用日 。夏令时 (DST) 等异常意味着日子并不总是 24 小时。您列出的某些时间可能不存在于特定时区的特定日期。
如果您关心真实时刻、特定日期的有效时间,请使用 java.time.ZonedDateTime
(或 .OffsetDateTime
)。对于数据库传输,使用 java.sql.Timestamp
.