获取系统当前时间戳 :
System.currentTimeMillis())
获取系统当前时间任意格式,自己根据生成的格式选择性填写
/**
* 获取当前时间
* 把需要生成的时间格式替换一下就可以
* @return
*/
public static String getCurrentTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
SimpleDateFormat sdf1 = new SimpleDateFormat("MM月dd日");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy/MM/dd,HH:mm");
SimpleDateFormat sdf5 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
SimpleDateFormat sdf6 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
SimpleDateFormat sdf7 = new SimpleDateFormat("yyyyMMdd HH:mm");
SimpleDateFormat sdf8 = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat sdf9 = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdf10 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf11 = new SimpleDateFormat("yyyy/MM/dd");
System.out.println(sdf.format(new java.util.Date()));
System.out.println(sdf1.format(new java.util.Date()));
System.out.println(sdf2.format(new java.util.Date()));
System.out.println(sdf3.format(new java.util.Date()));
System.out.println(sdf4.format(new java.util.Date()));
System.out.println(sdf5.format(new java.util.Date()));
System.out.println(sdf6.format(new java.util.Date()));
System.out.println(sdf7.format(new java.util.Date()));
System.out.println(sdf8.format(new java.util.Date()));
System.out.println(sdf9.format(new java.util.Date()));
System.out.println(sdf10.format(new java.util.Date()));
System.out.println(sdf11.format(new java.util.Date()));
return sdf.format(new java.util.Date());
}
输出结果:
2021年12月04日17时11分55秒
12月04日
2021-12-04-17-11-55
2021年12月04日 17:11
2021/12/04,17:11
2021/12/04 17:11
2021-12-04 17:11
20211204 17:11
17:11:55
20211204
2021-12-04
2021/12/04
任意时间日期转时间戳
/**
* 按照日期格式生成时间戳
* @param date
* @return 1638605460000
*/
public static Long dateToMillis(String date){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd HH:mm");//20211204 17:11
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm");//2021-12-04-17-11
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");//2021年12月04日17时11分55秒
Long time = null;
try {
time = simpleDateFormat.parse(date).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return time ;
}
/**
* 获取 周一 二 三 四 五 六 日
* @param timeStamp
* @return
*/
private static String getWeek(long timeStamp) {
int mydate = 0;
String week = null;
Calendar cd = Calendar.getInstance();
cd.setTime(new Date(timeStamp));
mydate = cd.get(Calendar.DAY_OF_WEEK);
// 获取指定日期转换成星期几
if (mydate == 1) {
week = "周日";
} else if (mydate == 2) {
week = "周一";
} else if (mydate == 3) {
week = "周二";
} else if (mydate == 4) {
week = "周三";
} else if (mydate == 5) {
week = "周四";
} else if (mydate == 6) {
week = "周五";
} else if (mydate == 7) {
week = "周六";
}
return week;
}
获取带有周几的时间格式
/**
* 调用此方法输入所要转换的时间戳例如(1638605460)输出("2021年12月04日 周二 16:11")
* 如果周几想变换位置的话 只需挪动 # 号即可
* @param timeStamp
* @return
*/
public static String times(long timeStamp) {
// SimpleDateFormat sdr = new SimpleDateFormat("# yyyy年MM月dd日 HH:mm");//周二 2021年12月04日 16:11
SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日 # HH:mm");//2021年12月04日 周二 16:11
return sdr.format(new Date(timeStamp*1000)).replaceAll("#",getWeek(timeStamp));
}
任意时间格式转换时间数组
/**
* 并用分割符把时间分成时间数组
*2021年12月04日17时02分07秒 输出结果为 [2021, 12, 04, 17, 02, 07]
* @param time
* @return
*/
public static String[] timestamp(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
String times = sdr.format(new Date());
String[] fenge = times.split("[年月日时分秒]");
System.out.println(Arrays.toString(fenge));//[2021, 12, 04, 17, 02, 07]
return fenge;
}