import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class HelloWorld {
public static void main(String []args) {
System.out.println("Hello World!");
byte[] bytes={0x02, 0x05 ,0x04,0x03 ,0x01 ,0x00 ,0x36 ,0x63 ,0x65 ,0x32 ,0x64 ,0x65 ,0x35 ,0x37 ,0x30 ,0x64};
String str="int: "+ onlineTime(bytes[2],bytes[0],bytes[3],bytes[4],bytes[4],bytes[4]);
System.out.println("Hello World!"+str);
}
public static long onlineTime(byte year, byte month, byte day, byte hour, byte minute, byte second) {
try {
String devTime = "20" + formatNum(year) +
"-" + formatNum(month) +
"-" + formatNum(day) +
" " + formatNum(hour) +
":" + formatNum(minute) +
":" + formatNum(second);
String devTimeint = "";
System.out.println("onlineTime:"+devTime);
devTimeint = dateToStamp(devTime);
System.out.println("onlineTime:"+devTimeint);
if (devTimeint.equals("")) {
return 0;
}
return Long.parseLong(devTimeint);
} catch (Exception e) {
e.getMessage();
return 0;
}
}
//个位数时前面补0
public static String formatNum(Byte num) {
DecimalFormat decimalFormat = new DecimalFormat("00");
return decimalFormat.format(num & 0xFF);
}
//将时间转换为时间戳
public static String dateToStamp(String s) throws Exception {
String res;
//设置时间格式,将该时间格式的时间转换为时间戳
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(s);
long time = date.getTime();
res = String.valueOf(time);
return res;
}
//将时间戳转换为时间
public static String stampToTime(String s) throws Exception {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lt = new Long(s);
//将时间戳转换为时间
Date date = new Date(lt);
//将时间调整为yyyy-MM-dd HH:mm:ss时间样式
res = simpleDateFormat.format(date);
return res;
}
}
转换时间挫
最新推荐文章于 2024-11-03 11:01:20 发布