package com.hdl.photovoltaic.utils;
|
|
import android.annotation.SuppressLint;
|
|
import java.text.SimpleDateFormat;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.Locale;
|
|
/**
|
* 时间工具类
|
*/
|
public class TimeUtils {
|
|
/**
|
* 时间戳转时间(格式:yyyy-MM-dd HH:mm:ss)
|
*
|
* @param timestamp 时间戳
|
* @return -
|
*/
|
public static String getTimeFromTimestamp(long timestamp) {
|
try {
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", 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 "";
|
}
|
|
}
|