From b78bd398ea618c6f884b06832e59bfb0a38be43f Mon Sep 17 00:00:00 2001 From: xufuji456 <839789740@qq.com> Date: Sat, 30 Jan 2021 22:03:30 +0800 Subject: [PATCH] change TimeUtil to kotlin --- .../java/com/frank/ffmpeg/util/TimeUtil.java | 82 ------------------- .../java/com/frank/ffmpeg/util/TimeUtil.kt | 82 +++++++++++++++++++ .../frank/ffmpeg/view/VideoPreviewBar.java | 4 +- 3 files changed, 84 insertions(+), 84 deletions(-) delete mode 100644 app/src/main/java/com/frank/ffmpeg/util/TimeUtil.java create mode 100644 app/src/main/java/com/frank/ffmpeg/util/TimeUtil.kt diff --git a/app/src/main/java/com/frank/ffmpeg/util/TimeUtil.java b/app/src/main/java/com/frank/ffmpeg/util/TimeUtil.java deleted file mode 100644 index ede7d47..0000000 --- a/app/src/main/java/com/frank/ffmpeg/util/TimeUtil.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.frank.ffmpeg.util; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Locale; - -/** - * the tool of time transforming - * Created by frank on 2018/11/12. - */ - -public class TimeUtil { - - private static final String YMDHMS = "yyyy-MM-dd HH:mm:ss"; - - /** - * convert timestramp into String - * - * @param time time - * @return yyyy/MM/dd HH:mm:ss - */ - public static String getDetailTime(long time) { - SimpleDateFormat format = new SimpleDateFormat(YMDHMS, Locale.getDefault()); - Date date = new Date(time); - return format.format(date); - } - - /** - * convert normal time into timestamp - * - * @param time time - * @return timestamp - */ - public static long getLongTime(String time, Locale locale) { - SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YMDHMS, locale); - try { - Date dt = simpleDateFormat.parse(time); - return dt.getTime(); - } catch (ParseException e) { - e.printStackTrace(); - } - return 0; - } - - private static String addZero(int time) { - if (time >= 0 && time < 10) { - return "0" + time; - } else if (time >= 10) { - return "" + time; - } else { - return ""; - } - } - - /** - * convert timestamp into video time - * - * @param time time - * @return video time - */ - public static String getVideoTime(long time) { - if (time <= 0) - return null; - time = time / 1000; - int second, minute = 0, hour = 0; - second = (int) time % 60; - time = time / 60; - if (time > 0) { - minute = (int) time % 60; - hour = (int) time / 60; - } - if (hour > 0) { - return addZero(hour) + ":" + addZero(minute) + ":" + addZero(second); - } else if (minute > 0) { - return addZero(minute) + ":" + addZero(second); - } else { - return "00:" + addZero(second); - } - } - -} diff --git a/app/src/main/java/com/frank/ffmpeg/util/TimeUtil.kt b/app/src/main/java/com/frank/ffmpeg/util/TimeUtil.kt new file mode 100644 index 0000000..af743b5 --- /dev/null +++ b/app/src/main/java/com/frank/ffmpeg/util/TimeUtil.kt @@ -0,0 +1,82 @@ +package com.frank.ffmpeg.util + +import java.text.ParseException +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale + +/** + * the tool of time transforming + * Created by frank on 2018/11/12. + */ + +object TimeUtil { + + private const val YMDHMS = "yyyy-MM-dd HH:mm:ss" + + /** + * convert timestramp into String + * + * @param time time + * @return yyyy/MM/dd HH:mm:ss + */ + fun getDetailTime(time: Long): String { + val format = SimpleDateFormat(YMDHMS, Locale.getDefault()) + val date = Date(time) + return format.format(date) + } + + /** + * convert normal time into timestamp + * + * @param time time + * @return timestamp + */ + fun getLongTime(time: String, locale: Locale): Long { + val simpleDateFormat = SimpleDateFormat(YMDHMS, locale) + try { + val dt = simpleDateFormat.parse(time) + return dt.time + } catch (e: ParseException) { + e.printStackTrace() + } + + return 0 + } + + private fun addZero(time: Int): String { + return when { + time in 0..9 -> "0$time" + time >= 10 -> "" + time + else -> "" + } + } + + /** + * convert timestamp into video time + * + * @param t time + * @return video time + */ + fun getVideoTime(t: Long): String? { + var time = t + if (time <= 0) + return null + time /= 1000 + val second: Int + var minute = 0 + var hour = 0 + second = time.toInt() % 60 + time /= 60 + if (time > 0) { + minute = time.toInt() % 60 + hour = time.toInt() / 60 + } + return when { + hour > 0 -> addZero(hour) + ":" + addZero(minute) + ":" + addZero(second) + minute > 0 -> addZero(minute) + ":" + addZero(second) + else -> "00:" + addZero(second) + } + } + +} diff --git a/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java b/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java index d9a192c..9a809c6 100644 --- a/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java +++ b/app/src/main/java/com/frank/ffmpeg/view/VideoPreviewBar.java @@ -169,7 +169,7 @@ public class VideoPreviewBar extends RelativeLayout implements HardwareDecode.On @Override public void run() { previewBar.setMax(durationMs); - txtVideoDuration.setText(TimeUtil.getVideoTime(durationMs)); + txtVideoDuration.setText(TimeUtil.INSTANCE.getVideoTime(durationMs)); texturePreView.setVisibility(GONE); } }); @@ -198,7 +198,7 @@ public class VideoPreviewBar extends RelativeLayout implements HardwareDecode.On public void updateProgress(int progress) { if (progress >= 0 && progress <= duration) { - txtVideoProgress.setText(TimeUtil.getVideoTime(progress)); + txtVideoProgress.setText(TimeUtil.INSTANCE.getVideoTime(progress)); previewBar.setProgress(progress); } }