时间格式转化

秒数转 xx day,HH:mm:ss 格式

public static String getTimeBySecond(String time){
		if (!"".equals(time) && null != time) {
			int str = Integer.parseInt(time);
			int day = str / (24 * 60 * 60);
			int hour = str % (24 * 60 * 60) / (60 * 60);
			int min = str % (24 * 60 * 60) % (60 * 60) / 60;
			int second = str % (24 * 60 * 60) % (60 * 60) % 60;
			String h = String.valueOf(hour);
			String m = String.valueOf(min);
			String s = String.valueOf(second);
			String resday ="";
			if(day<=0){
				resday="";
			}else{
				resday=day+ "day,";
			}
			if (hour >= 0 && hour < 10) {
				h = "0" + hour;
			}
			if (min >= 0 && min < 10) {
				m = "0" + min;
			}
			if (second >= 0 && second < 10) {
				s = "0" + second;
			}
			return resday + h + ":" + m + ":" + s;
		}
			return null;

	}

获取上个月的时间

 public static String getLastMonth(String time){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.add(Calendar.MONTH, -1);
        Date m = c.getTime();
        String mon = format.format(m);
        return mon;
    }

utc转北京时间

 public static  String changeTimes(String times){
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                calendar.setTime(format.parse(times));
                calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + 8);
//                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
                //calendar.getTime() 返回的是Date类型,也可以使用calendar.getTimeInMillis()获取时间戳
//                System.out.println("北京时间: " + calendar.getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
           return sdf.format(calendar.getTime());

        }

获取某月的最后一天

public static String getLastDayOfMonth(int year,int month)	{
		Calendar cal = Calendar.getInstance();		//设置年份
		cal.set(Calendar.YEAR,year);		//设置月份
		cal.set(Calendar.MONTH, month-1);		//获取某月最大天数
		int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);		//设置日历中月份的最大天数
		cal.set(Calendar.DAY_OF_MONTH, lastDay);		//格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String lastDayOfMonth = sdf.format(cal.getTime());
		 return lastDayOfMonth;
		 } 

按5分钟划分取整 6:03 取整为6:00 6:09取整为9:05

	public static Date getTimeBy5Minutes(Date date) {
		String dateStr = formatDate(date);
		String beforStr = dateStr.substring(0, 15);
		String afterStr = "";
		if (Integer.parseInt(dateStr.substring(15, 16)) >= 5) {
			afterStr = "5:00";
		} else {
			afterStr = "0:00";
		}
		return praseDate(beforStr + afterStr, "yyyy-MM-dd HH:mm:ss");
	}

bps转Kps,Mps,Gps

public static String getBpsOrPps(String b) {
		if (!"".equals(b) && null != b) {
			double value = Double.parseDouble(b);
			if (b.length() <= 3) {
				return value + "B";
			} else if (b.length() <= 6) {
				BigDecimal bb = new BigDecimal(value / 1000);
				double f1 = bb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				return f1 + "K";
			} else if (b.length() <= 9) {
				BigDecimal bb = new BigDecimal(value / 1000000);
				double f1 = bb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				return f1 + "M";
			} else {
				BigDecimal bb = new BigDecimal(value / 1000000000);
				double f1 = bb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				return f1 + "G";
			}
		}
			return null;

	}

猜你喜欢

转载自blog.csdn.net/qq_38631790/article/details/89468195