赞
踩
- /**
- * @Author: Be.insighted
- * Description:
- * @date Create on 2020/7/14 16:35
- **/
-
- public class TimeUtils{
-
- /**
- * 把时间戳转换为:时分秒
- *
- * @param millisecond :毫秒,传入单位为毫秒
- */
- public static String getTimeString(final long millisecond) {
- if (millisecond < 1000) {
- return "0" + "秒";
- }
- long second = millisecond / 1000;
- long seconds = second % 60;
- long minutes = second / 60;
- long hours = 0;
- if (minutes >= 60) {
- hours = minutes / 60;
- minutes = minutes % 60;
- }
- String timeString = "";
- String secondString = "";
- String minuteString = "";
- String hourString = "";
- if (seconds < 10) {
- secondString = "0" + seconds + "秒";
- } else {
- secondString = seconds + "秒";
- }
- if (minutes < 10 && hours < 1) {
- minuteString = minutes + "分";
- } else if (minutes < 10){
- minuteString = "0" + minutes + "分";
- } else {
- minuteString = minutes + "分";
- }
- if (hours < 10) {
- hourString = hours + "时";
- } else {
- hourString = hours + "" + "时";
- }
- if (hours != 0) {
- timeString = hourString + minuteString + secondString;
- } else {
- timeString = minuteString + secondString;
- }
- return timeString;
- }
-
- public static Date convertTimeStamp2Date(long timeStamp) {
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
- Date date = new Date(timeStamp);
- try {
- date = df.parse(df.format(date));
- } catch (ParseException e) {
- log.error("时间格式化错误");
- }
- return date;
- }
-
- public static long dateToStamp(Date date, int hour, int minute, int second) {
- LocalDateTime timestamp = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
- return timestamp.withHour(hour).withMinute(minute).withSecond(second).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
- }
- }
- public class DateUtils {
-
- public static final String DATE_FORMAT_DATETIME_SIMPLE = "yyMMddHHmmss";
- /**
- * 年-月-日
- */
- public static final String DATE_FORMAT_DATEONLY = "yyyy-MM-dd";
-
- /**
- * 年-月-日
- */
- public static final String DATE_FORMAT_DATEONLY_MONTH = "yyyyMM";
- /**
- * 年-月-日 时分秒
- */
- public static final String DATETIME_FORMAT_DATEONLY = "yyyy-MM-dd HH:mm:ss";
-
- /**
- * 年-月-日 时分
- */
- public static final String DATE_FORMAT_DATE = "yyyy-MM-dd HH:mm";
-
- /**
- * dateTime formatter
- */
- public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern(DATETIME_FORMAT_DATEONLY);
- /**
- * date formatter
- */
- public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(DATE_FORMAT_DATEONLY);
-
- private static final ZoneId ZONE = ZoneId.systemDefault();
-
- public static String formatDateTimeString(long unixTime, String format) {
- if (unixTime == 0) {
- return "";
- }
- LocalDateTime dateTime = toLocalDateTime(unixTime);
- Date date = date(dateTime);
- SimpleDateFormat formator = new SimpleDateFormat(format);
- return formator.format(date);
- }
-
- public static String formatDateTimeString(long unixTime) {
- return formatDateTimeString(unixTime, DATETIME_FORMAT_DATEONLY);
- }
-
- /**
- * 毫秒转为时间 时:分:秒
- *
- * @param second
- * @return
- */
- public static String getGapTime(long second) {
- if (second != 0L) {
- second = second / 1000;
- }
- //转换天数
- long days = second / 86400;
- //剩余秒数
- second = second % 86400;
- //转换小时数
- long hours = second / 3600;
- //剩余秒数
- second = second % 3600;
- //转换分钟
- long minutes = second / 60;
- //剩余秒数
- second = second % 60;
- String s = days + "天" + hours + "时" + minutes + "分" + second + "秒";
- if (0 < days) {
- return s;
- } else {
- return hours + "时" + minutes + "分" + second + "秒";
- }
- }
-
- /**
- * 获取String类型时间
- *
- * @return
- */
- public static String convertCalender2String() {
- SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
- Calendar calendar = Calendar.getInstance();
- return df.format(calendar.getTime());
- }
-
- /**
- * java.time.LocalDateTime --> java.util.Date
- *
- * @param localDateTime
- * @return
- */
- public static Date date(LocalDateTime localDateTime) {
- if (localDateTime == null) {
- return null;
- }
- Instant instant = localDateTime.atZone(ZONE).toInstant();
- return Date.from(instant);
- }
-
-
- /**
- * 将字符串转换成日期,只到年月日
- * @param str
- * @return
- */
- public static Date strToDate(String str, String format) {
- try {
- if (StringUtils.isBlank(format)) {
- format = DATETIME_FORMAT_DATEONLY;
- }
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
- return simpleDateFormat.parse(str);
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
-
-
- /**
- * 将Date日期转换成String
- *
- * @param dateDate
- * @return
- */
- public static String dateToStr(Date dateDate, String formatter) {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatter);
- String dateString = simpleDateFormat.format(dateDate);
- return dateString;
- }
-
- /**
- * 将Date日期转换成String
- *
- * @param dateDate
- * @return
- */
- public static String dateToStr(Date dateDate) {
- SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT_DATEONLY);
- String dateString = formatter.format(dateDate);
- return dateString;
- }
-
-
- public static LocalDateTime toLocalDateTime(Date date){
-
- Instant instant = date.toInstant();
- ZoneId zone = ZoneId.systemDefault();
- return LocalDateTime.ofInstant(instant, zone);
- }
-
- public static LocalDateTime toLocalDateTime(long dateLong){
-
- return toLocalDateTime(new Date(dateLong));
- }
-
- public static Date toDate(LocalDateTime localDateTime){
-
- return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
- }
-
- public static Date toDate(LocalDate localDate){
-
- return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
- }
-
- public static Date toDate(LocalTime localTime) {
-
- LocalDate localDate = LocalDate.now();
- LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
- ZoneId zone = ZoneId.systemDefault();
- Instant instant = localDateTime.atZone(zone).toInstant();
- return Date.from(instant);
- }
-
- public static long toLong(LocalDateTime localDateTime){
-
- return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
- }
-
- public static long toLong(LocalTime localTime){
-
- LocalDate localDate = LocalDate.now();
- LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
- return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
- }
-
- public static long toLong(LocalDate localDate){
-
- LocalDateTime localDateTime = localDate.atStartOfDay();
- return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
- }
-
- public static boolean isToday(LocalDateTime datetime) {
- LocalDateTime now = LocalDateTime.now();
- return (now.getYear() == datetime.getYear()) && (now.getMonthValue() == datetime.getMonthValue()) && (now.getDayOfMonth() == datetime.getDayOfMonth());
- }
-
- }
- public static String secondToTime(int seconds) {
- if (seconds < 0) {
- throw new IllegalArgumentException("Seconds must be a positive number!");
- } else {
- int hour = seconds / 3600;
- int other = seconds % 3600;
- int minute = other / 60;
- int second = other % 60;
- StringBuilder sb = new StringBuilder();
- if (hour < 10) {
- sb.append("0");
- }
-
- sb.append(hour);
- sb.append(":");
- if (minute < 10) {
- sb.append("0");
- }
-
- sb.append(minute);
- sb.append(":");
- if (second < 10) {
- sb.append("0");
- }
-
- sb.append(second);
- return sb.toString();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。