2021年5月28日是星期五。现在从2021年5月28日零点开始计时,根据给定的计时毫秒数,求计时时刻的时间格式,并输出当时是星期几(其中星期日输出7)。 样例输入 86400001 样例输出 00:00:00 6 数据范围 n<1e16
public static void main(String[] args){ System.out.println(getTime(86400001L)); } private static String getTime(Long time){ try{ SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String fromDate = "2021-05-28 00:00:00"; //计算2021-05-28 00:00:00 的时间戳 long fromLong = sdf1.parse(fromDate).getTime(); //加上time,算目标时间的时间戳 long resLong = fromLong + time; //转换成date Date date = new Date(resLong); //是几点 SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss"); String clock = sdf2.format(date); //是星期几 int week = getWeek(date); return clock + " " + week; }catch (Exception e){ e.printStackTrace(); return ""; } } public static int getWeek(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1; if(week_index<0){ week_index = 7; } return week_index; }