version:1.0
update:2021-10-13 18:52:13 fix:去除okgo,rxAndroid1,优化依赖 add:切换到奥乐云平台
This commit is contained in:
177
app/src/main/java/com/aoleyun/sn/utils/TimeUtils.java
Normal file
177
app/src/main/java/com/aoleyun/sn/utils/TimeUtils.java
Normal file
@@ -0,0 +1,177 @@
|
||||
package com.aoleyun.sn.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import android.text.TextUtils;
|
||||
|
||||
|
||||
import com.aoleyun.sn.service.MainService;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
* 时间管控工具类
|
||||
*/
|
||||
public class TimeUtils {
|
||||
private static DateFormat df = new SimpleDateFormat("HH:mm");
|
||||
public static final String START_TIME_KEY = "START_TIME";
|
||||
public static final String END_TIME_KEY = "END_TIME";
|
||||
public static long dayTime = 60 * 60 * 24 * 1000;
|
||||
|
||||
public static String getNowTime() {
|
||||
long nowTime = System.currentTimeMillis();
|
||||
DateFormat df = ContralTime.getDf();
|
||||
return df.format(nowTime);
|
||||
}
|
||||
|
||||
// public static boolean CurrentInTimeScope(ContralTime contralTime) {
|
||||
// boolean result = true;
|
||||
// final long aDayInMillis = 1000 * 60 * 60 * 24;
|
||||
// long currentTimeMillis = System.currentTimeMillis();
|
||||
//
|
||||
// }
|
||||
|
||||
public static ContralTime String2ContralTime(Context context, @NonNull String timeText) {
|
||||
DateFormat df = ContralTime.getDf();
|
||||
String[] time = timeText.trim().split("-");
|
||||
if (time.length != 2) {
|
||||
throw new RuntimeException("Time format error!");
|
||||
}
|
||||
try {
|
||||
SPUtils.put(context, START_TIME_KEY, time[0].trim());
|
||||
SPUtils.put(context, END_TIME_KEY, time[1].trim());
|
||||
Date startDate = df.parse(time[0].trim());
|
||||
Date endDate = df.parse(time[1].trim());
|
||||
ContralTime contralTime = new ContralTime();
|
||||
// if (date1.getTime() < date2.getTime()) {
|
||||
contralTime.setStartTime(df.format(startDate));
|
||||
contralTime.setEndTime(df.format(endDate));
|
||||
// } else {
|
||||
// contralTime.setStartTime(df.format(date2));
|
||||
// contralTime.setEndTime(df.format(date1));
|
||||
// }
|
||||
return contralTime;
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static ContralTime getDefaltContralTime(Context context) {
|
||||
String startTime = (String) SPUtils.get(context, START_TIME_KEY, "00:00");
|
||||
String endTime = (String) SPUtils.get(context, END_TIME_KEY, "00:00");
|
||||
if (null == startTime || null == endTime || ("00:00".equals(startTime) && "00:00".equals(endTime))) {
|
||||
return null;
|
||||
} else {
|
||||
try {
|
||||
Date startDate = df.parse(startTime.trim());
|
||||
Date endDate = df.parse(endTime.trim());
|
||||
ContralTime contralTime = new ContralTime();
|
||||
contralTime.setStartTime(df.format(startDate));
|
||||
contralTime.setEndTime(df.format(endDate));
|
||||
return contralTime;
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void setEmpty(Context context) {
|
||||
SPUtils.put(context, START_TIME_KEY, "00:00");
|
||||
SPUtils.put(context, END_TIME_KEY, "00:00");
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(MainService.TimeChangedReceiver.ACTION_UPDATE);
|
||||
context.sendBroadcast(intent);
|
||||
}
|
||||
|
||||
|
||||
public static class ContralTime {
|
||||
|
||||
//format HH:mm
|
||||
String startTime;
|
||||
String endTime;
|
||||
|
||||
public ContralTime() {
|
||||
|
||||
}
|
||||
|
||||
public ContralTime(String startT, String endT) {
|
||||
this.startTime = startT;
|
||||
endTime = endT;
|
||||
}
|
||||
|
||||
public String getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
|
||||
public void setStartTime(String startT) {
|
||||
this.startTime = startT;
|
||||
}
|
||||
|
||||
public String getEndTime() {
|
||||
return endTime;
|
||||
}
|
||||
|
||||
public void setEndTime(String endT) {
|
||||
this.endTime = endT;
|
||||
}
|
||||
|
||||
public static DateFormat getDf() {
|
||||
return df;
|
||||
}
|
||||
|
||||
public static void setDf(DateFormat d) {
|
||||
df = d;
|
||||
}
|
||||
|
||||
public String getNowTimeString(long time) {
|
||||
return df.format(new Date(time));
|
||||
}
|
||||
|
||||
public boolean inControlTime(long time) {
|
||||
return inControlTime(df.format(new Date(time)));
|
||||
}
|
||||
|
||||
public boolean inControlTime(String time) {
|
||||
if (TextUtils.isEmpty(time)) {
|
||||
throw new RuntimeException("Time is empty");
|
||||
} else {
|
||||
if (!time.contains(":")) {
|
||||
throw new RuntimeException("Time format error");
|
||||
}
|
||||
}
|
||||
try {
|
||||
Date startDate = df.parse(startTime);
|
||||
Date endDate = df.parse(endTime);
|
||||
Date nowDate = df.parse(time);
|
||||
if (startDate.getTime() > endDate.getTime()) {
|
||||
//开始时间大于结束时间 列 16:00-01:00
|
||||
endDate.setTime(endDate.getTime() + dayTime);
|
||||
}
|
||||
if (nowDate.getTime() >= startDate.getTime() && nowDate.getTime() <= endDate.getTime()) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public String toString() {
|
||||
return startTime + "\t-\t" + endTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user