我的日期时间类型如下表
private void releaseMediaRecorder() {
if (mediaRecorder != null) {
mediaRecorder.reset(); // clear recorder configuration
mediaRecorder.release(); // release the recorder object
mediaRecorder = null;
camera.lock(); // lock camera for later use
}
}
private boolean prepareMediaRecorder() {
mediaRecorder = new MediaRecorder();
camera.unlock();
mediaRecorder.setCamera(camera);
mediaRecorder.setOnInfoListener(_instance.get());
mediaRecorder.setOnErrorListener(_instance.get());
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
CamcorderProfile cf = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
if (CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_TIME_LAPSE_1080P)) {
cf = CamcorderProfile.get(CamcorderProfile.QUALITY_TIME_LAPSE_1080P);
}
mediaRecorder.setProfile(cf);
File videoDirectory = FileUtility.getCameraDocsFolder(_instance.get(), 2);
if (!videoDirectory.exists()) {
videoDirectory.mkdirs();
}
currentVideoUri = videoDirectory.getAbsolutePath() + "/" + FileUtility.getAttachmentFileName(2);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setOrientationHint(curentRotation);
mediaRecorder.setOutputFile(currentVideoUri);
mediaRecorder.setMaxDuration(Constants.MAX_DURATION_RECORD); // Set max duration 60 sec.
mediaRecorder.setMaxFileSize(Constants.MAX_FILE_SIZE_RECORD); // Set max file size 50M
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}
我需要在给定的输入时间内获得过去24小时内所有错过的interval_time。
我需要得到表格中没有的所有错过的时间间隔,如下所示。
Fatal Exception: java.lang.IllegalStateException
at android.media.MediaRecorder.setAudioEncoder(MediaRecorder.java)
at com.ms.engage.ui.CameraActivity.prepareMediaRecorder(SourceFile:976)
at com.ms.engage.ui.CameraActivity.startVideoRecording(SourceFile:1199)
at com.ms.engage.ui.CameraActivity.onTouch(SourceFile:910)
at android.view.View.dispatchTouchEvent(View.java:9138)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2676)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2347)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2687)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2362)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2687)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2362)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2687)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2362)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2687)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2362)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2687)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2362)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2687)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2362)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2687)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2362)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2687)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2362)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2739)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1832)
at android.app.Activity.dispatchTouchEvent(Activity.java:2979)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2700)
at android.view.View.dispatchPointerEvent(View.java:9354)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4975)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4736)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4268)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4321)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4287)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4413)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4295)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4470)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4268)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4321)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4287)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4295)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4268)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6865)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6746)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6717)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6955)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(InputEventReceiver.java)
at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6926)
at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6978)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:558)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6838)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
在这里,我可以使用过去24小时的所有间隔列表加入表格,但是如何以小时为间隔获取最后24小时列表。
答案 0 :(得分:1)
假设'2016-07-22 22:19:00'是您要求的日期时间。然后您可以使用以下查询来使用最后24小时状态。
数据库结构
CREATE TABLE IF NOT EXISTS `mytable` (
`Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`interval_time` datetime NOT NULL,
`processed_time` datetime NOT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
--
-- Dumping data for table `mytable`
--
INSERT INTO `mytable` (`Id`, `interval_time`, `processed_time`) VALUES
(1, '2016-07-22 08:00:00', '2016-07-22 09:25:00'),
(2, '2016-07-22 09:00:00', '2016-07-22 10:30:00'),
(3, '2016-07-22 14:00:00', '2016-07-22 15:23:00'),
(4, '2016-07-22 15:00:00', '2016-07-22 14:27:00'),
(5, '2016-07-22 16:00:00', '2016-07-22 15:24:00'),
(6, '2016-07-22 17:00:00', '2016-07-22 16:40:00');
<强> QUERY 强>
SELECT A.mydate FROM(
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 1 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 2 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 3 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 4 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 5 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 6 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 7 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 8 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 9 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 10 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 11 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 12 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 13 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 14 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 15 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 16 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 17 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 18 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 19 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 20 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 21 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 22 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 23 HOUR,"%Y-%m-%d %H:00:00") mydate
UNION
SELECT DATE_FORMAT('2016-07-22 22:19:00' - INTERVAL 24 HOUR,"%Y-%m-%d %H:00:00") mydate
) A LEFT JOIN `mytable` B ON A.mydate = B.`interval_time` WHERE B.Id IS NULL ORDER BY mydate DESC;
检查SQL Fiddle
为了减少上述查询的复杂性,可以按如下方式使用存储过程调用。
DELIMITER $$
CREATE PROCEDURE `LastHrs`(IN indate DATETIME)
BEGIN
DECLARE X INT;
DECLARE str TEXT;
SET X = 2;
SET str = CONCAT('SELECT DATE_FORMAT("',indate,'" - INTERVAL 1 HOUR,"%Y-%m-%d %H:00:00") mydate');
WHILE X <= 24 DO
SET str = CONCAT(str,' UNION ','SELECT DATE_FORMAT("',indate,'" - INTERVAL ',X,' HOUR,"%Y-%m-%d %H:00:00") mydate');
SET X = X + 1;
END WHILE;
SET @query = CONCAT('SELECT A.mydate FROM(',str,') A LEFT JOIN `mytable` B ON A.mydate = B.`interval_time` WHERE B.Id IS NULL ORDER BY mydate DESC');
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;
您可以使用查询
来调用存储过程CALL LastHrs('2016-07-22 22:19:00');