2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > java 时间转换工具类 yyyyMMdd HH:mm

java 时间转换工具类 yyyyMMdd HH:mm

时间:2023-08-11 06:41:04

相关推荐

java 时间转换工具类 yyyyMMdd HH:mm

获取系统当前时间戳 :

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());}

输出结果:

12月04日17时11分55秒

12月04日

-12-04-17-11-55

12月04日 17:11

/12/04,17:11

/12/04 17:11

-12-04 17:11

1204 17:11

17:11:55

1204

-12-04

/12/04

任意时间日期转时间戳

/*** 按照日期格式生成时间戳* @param date* @return 1638605460000*/public static Long dateToMillis(String date){SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd HH:mm");//1204 17:11// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm");//-12-04-17-11// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");//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)输出("12月04日 周二 16:11")* 如果周几想变换位置的话 只需挪动 # 号即可* @param timeStamp* @return*/public static String times(long timeStamp) {// SimpleDateFormat sdr = new SimpleDateFormat("# yyyy年MM月dd日 HH:mm");//周二 12月04日 16:11SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日 # HH:mm");//12月04日 周二 16:11return sdr.format(new Date(timeStamp*1000)).replaceAll("#",getWeek(timeStamp));}

任意时间格式转换时间数组

/*** 并用分割符把时间分成时间数组*12月04日17时02分07秒 输出结果为 [, 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));//[, 12, 04, 17, 02, 07]return fenge;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。