package com.hxzkoa.udp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; public class GetNowTime { /** 显示年月日时分秒2020-11-23 21:47:45 */ public static String now() {// 获取时间方法 Date date = new Date();// 日期对象 String GreatTime1 = String.format("%tF", date);// 格式时间年月日 String GreatTime2 = String.format("%tT", date);// 格式时间时分 String GreatTime = GreatTime1 + " " + GreatTime2;// 以年月日 时分秒显示 date = null; return GreatTime; } /** 获取年月日时间 格式为20210309 */ public static String getyearmd() { Date date = new Date();// 日期对象 String GreatTime1 = String.format("%tF", date);// 格式时间年月日 String time = GreatTime1.replace("-", ""); return time; } /** 增加或者减去n天后的日期 */ public static String addDay(int n) { try { Date date = new Date();// 日期对象 String GreatTime1 = String.format("%tF", date);// 格式时间年月日 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar cd = Calendar.getInstance(); cd.setTime(sdf.parse(GreatTime1)); cd.add(Calendar.DATE, n);// 增加一天 // cd.add(Calendar.MONTH, n);//增加一个月 String time = sdf.format(cd.getTime()).replace("-", ""); return time; } catch (Exception e) { return null; } } /** 增加或者减去n天后的日期 */ public static String addDay1(int n) { try { Date date = new Date();// 日期对象 String GreatTime1 = String.format("%tF", date);// 格式时间年月日 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar cd = Calendar.getInstance(); cd.setTime(sdf.parse(GreatTime1)); cd.add(Calendar.DATE, n);// 增加一天 String time = sdf.format(cd.getTime()); return time; } catch (Exception e) { return null; } } /** 显示年月日时分秒2020-11-23_21:47:45 */ public static String nowfinename() {// 获取时间方法 Date date = new Date();// 日期对象 String GreatTime1 = String.format("%tF", date);// 格式时间年月日 String GreatTime2 = String.format("%tT", date);// 格式时间时分 String GreatTime = GreatTime1 + "_" + GreatTime2;// 以年月日 时分秒显示 date = null; return GreatTime; } /** 获取年月日时间2020-11-23 */ public static String y_m_d() {// 获取时间方法 Date date = new Date();// 日期对象 String GreatTime1 = String.format("%tF", date);// 格式时间年月日 return GreatTime1; } /** 时间格式 年-月-日 时:分:秒 */ public static String getTimeDay(int index) { Calendar calendar = Calendar.getInstance(); SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); calendar.add(Calendar.DAY_OF_MONTH, index); String date = fmt.format(calendar.getTime()); return date; } /** 获取当前时间的时分秒 形如:19:19:20 */ public static String HH_MM_SS() {// 获取时间方法 Date date = new Date();// 日期对象 String hms = String.format("%tT", date);// 格式时间时分 String GreatTime = hms; return GreatTime; } /** 时间格式月-日 时分秒11-23 21:48:48 **/ public static String now2() {// 获取时间方法 Date date = new Date();// 日期对象 String mounth = String.format("%tm", date);// 格式时间时分 String day = String.format("%td", date);// 格式时间年月日 String hms = String.format("%tT", date);// 格式时间时分 String GreatTime = mounth + "-" + day + " " + hms; return GreatTime; } /** 获取年月日时分秒毫秒时间 2020-05-30 12:33:45.23 */ public static String timestamp2() { String d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(Calendar.getInstance().getTime()); return d; } /** 获取年月日时分秒毫秒时间 2020-05-30 12:33:45.23 */ @SuppressWarnings("unused") public static String gethm() { Date date = new Date();// 日期对象 String mounth = String.format("%tm", date);// 格式时间时分 String day = String.format("%td", date);// 格式时间年月日 String hms = String.format("%tT", date);// 格式时间时分 String GreatTime = hms; return GreatTime; } /** 获取毫秒时间戳21:49:27.519 */ public static String sss() {// 获取时间方法 String msg = ""; Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS"); msg += sdf.format(date); return msg; } /** * 获取JAN需要的时间戳timestamp时间戳 (小时*3600+分钟*60+秒)*1000 +毫秒 举例: 78598245 */ public static int timestamp() { int time = 0; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); String inputString = sss(); Date date = null; try { date = sdf.parse("1970-01-01 " + inputString); } catch (ParseException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } time = (int) date.getTime(); return time; } /** 将年月日时间戳转为毫秒整数 */ public static int timestamp(String timestamp) { int time = 0; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = null; try { date = sdf.parse("1970-01-01 " + timestamp.substring(11, 22)); } catch (ParseException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } time = (int) date.getTime(); return time; } }