package com.hdl.photovoltaic.utils;
|
|
import android.annotation.SuppressLint;
|
|
import com.hdl.photovoltaic.config.UserConfigManage;
|
import com.hdl.photovoltaic.other.HdlThreadLogic;
|
import com.hdl.sdk.link.core.utils.LanguageUtils;
|
|
import java.text.SimpleDateFormat;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.Locale;
|
|
/**
|
* 时间工具类
|
*/
|
public class TimeUtils {
|
|
public static final String zhDateFormat = "yyyy/MM/dd";
|
public static final String enDateFormat = "dd/MM/yyyy";
|
|
/**
|
* 获取分钟
|
*
|
* @param timestamp 时间戳
|
* @return -
|
*/
|
public static String getMinuteTime(long timestamp) {
|
try {
|
SimpleDateFormat dateFormat = new SimpleDateFormat("mm", Locale.ENGLISH);
|
Date date = new Date(timestamp);
|
return dateFormat.format(date);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return "";
|
}
|
|
/**
|
* 时间戳转时间(格式:yyyy-MM-dd)
|
*
|
* @param timestamp 时间戳
|
* @param date_format 时间格式(例如"yyyy-MM-dd")
|
* @return -
|
*/
|
public static String getDateTimestamp(long timestamp, String date_format) {
|
try {
|
SimpleDateFormat dateFormat = new SimpleDateFormat(date_format, Locale.ENGLISH);
|
Date date = new Date(timestamp);
|
return dateFormat.format(date);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return "";
|
}
|
|
/**
|
* 时间戳转时间(格式:yyyy-MM-dd HH:mm:ss)
|
*
|
* @param timestamp 时间戳
|
* @return -
|
*/
|
public static String getTimeFromTimestamp(long timestamp) {
|
try {
|
String pattern = "yyyy-MM-dd HH:mm";
|
if (UserConfigManage.getInstance().getCurrentAppLanguage().equals(LocalManageUtil.en)) {
|
pattern = "dd-MM-yyyy HH:mm";
|
}
|
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.ENGLISH);
|
Date date = new Date(timestamp);
|
return dateFormat.format(date);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return "";
|
}
|
|
/**
|
* 时间戳转时间(格式:yyyy-MM-dd HH:mm:ss)
|
*
|
* @param timestamp 时间戳
|
* @return -
|
*/
|
@SuppressLint("DefaultLocale")
|
public static String getCalendarTimeFromTimestamp(long timestamp) {
|
try {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTimeInMillis(timestamp);
|
int year = calendar.get(Calendar.YEAR);
|
int month = calendar.get(Calendar.MONTH) + 1; // 月份从0开始,所以要加1
|
int day = calendar.get(Calendar.DAY_OF_MONTH);
|
int hour = calendar.get(Calendar.HOUR_OF_DAY);
|
int minute = calendar.get(Calendar.MINUTE);
|
int second = calendar.get(Calendar.SECOND);
|
return String.format("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return "";
|
}
|
|
/**
|
* 加天数
|
*
|
* @param date 日历
|
* @param daysToAdd 天数
|
* @return 日历
|
*/
|
public static Calendar addDaysToDate(Calendar date, int daysToAdd) {
|
Calendar newDate = (Calendar) date.clone();
|
newDate.add(Calendar.DAY_OF_MONTH, daysToAdd);
|
return newDate;
|
}
|
|
/**
|
* 减天数
|
*
|
* @param date 日历
|
* @param daysToSubtract 天数
|
* @return 日历
|
*/
|
public static Calendar subtractDaysFromDate(Calendar date, int daysToSubtract) {
|
Calendar newDate = (Calendar) date.clone();
|
newDate.add(Calendar.DAY_OF_MONTH, -daysToSubtract);
|
return newDate;
|
}
|
|
|
}
|