java得到当前时间java怎么获取当前系统时间?

java得到当前时间  时间:2021-07-15  阅读:()

在Java中获得当前系统时间?

// 获得当前系统时间 public String getNowtimeTwo() { Calendar rightNow = Calendar.getInstance(); SimpleDateFormat fmt = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");//格式大小写有区别 String sysDatetime = fmt.format(rightNow.getTime()); return sysDatetime; }

java 获取当前日期,应该如何操作呢

package util; import java.math.BigDecimal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 获取系统时间 * */ public class DateUtil { /* 日志对象 */ // private static Logger logger = Logger.getLogger(SystemUtil.class); /* 获取年份 */ public static final int YEAR = 1; /* 获取年月 */ public static final int YEARMONTH = 2; /* 获取年月日 */ public static final int YEARMONTHDAY = 3; /* 获取年月日,小时 */ public static final int YMD_HOUR = 4; /* 获取年月日,小时,分钟 */ public static final int YMD_HOURMINUTE = 5; /* 获取年月日,时分秒 */ public static final int FULL = 6; /* 获取年月日时分秒 格式:yyyyMMddHHmmss */ public static final int UTILTIME = 7; /** * 根据指定时间格式类型得到当前时间 * * @param type * 时间类型 * @return String 字符串时间 */ public static synchronized String getCurrentTime(int type) { String format = getFormat(type); SimpleDateFormat timeformat = new SimpleDateFormat(format); Date date = new Date(); return timeformat.format(date); } /** * 返回当前系统时间的年月日 * * @return */ public static synchronized String getCurrentTime() { SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd"); Date date = new Date(); return timeformat.format(date); } /** * 根据参数格式,格式化当前日期 * @param format * @return */ public static synchronized String getDateString(String format) { SimpleDateFormat timeformat = new SimpleDateFormat(format); Date date = new Date(); return timeformat.format(date); } /** * 根据指定时间格式类型,格式化时间格式 * * @param type * 时间格式类型 * @return */ private static String getFormat(int type) { String format = ""; if (type == 1) { format = "yyyy"; } else if (type == 2) { format = "yyyy-MM"; } else if (type == 3) { format = "yyyy-MM-dd"; } else if (type == 4) { format = "yyyy-MM-dd HH"; } else if (type == 5) { format = "yyyy-MM-dd HH:mm"; } else if (type == 6) { format = "yyyy-MM-dd HH:mm:ss"; } else if (type == 7) { format = "yyyyMMddHHmmss"; } else { throw new RuntimeException("日期格式参数错误"); } return format; } public static int getYear(String dateString) { SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = dd.parse(dateString); Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal.get(Calendar.YEAR); } catch (Exception e) { throw new RuntimeException(e); } } public static int getMonth(String dateString) { SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = dd.parse(dateString); Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal.get(Calendar.MONTH)+1; } catch (Exception e) { throw new RuntimeException(e); } } public static int getDay(String dateString) { SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = dd.parse(dateString); Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal.get(Calendar.DAY_OF_MONTH); } catch (Exception e) { throw new RuntimeException(e); } } public static Date StringToDate(String dateStr, String formatStr) { SimpleDateFormat dd = new SimpleDateFormat(formatStr); Date date = null; try { date = dd.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } return date; } /** * 当前日期和参数日期距离的小时数 日期格式:yyyy-MM-dd HH:mm:ss * * @param date * @return */ public static double getHours(String date) { SimpleDateFormat timeformat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); try { Date d = new Date(); Date d1 = timeformat.parse(date); long temp = d.getTime() - d1.getTime(); double f = temp / 3600000d; BigDecimal b = new BigDecimal(f); double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); return f1; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } public static void main(String a[]) { try { int aa = getYear("2012-01-08"); System.out.println(aa); } catch (Exception e) { e.printStackTrace(); } } }

java中获取当前时间的前几个月

看你描述的不是很清楚,看看下面是不是你想要的。

import?java.text.DateFormat; import?java.text.SimpleDateFormat; import?java.util.Calendar; import?java.util.Date; public?class?DateUtils?{ ????private?static?final?DateFormat?DATE_FORMAT?=?new?SimpleDateFormat("yyyy-MM-dd?HH:mm:ss"); ????public?static?void?main(String[]?args)?{ ????????Date?now?=?new?Date(); ????????System.out.println("当前日期:"?+?DATE_FORMAT.format(now)); ????????Date?newDate?=?stepMonth(now,?-13); ????????System.out.println("当前时间前13个月的日期:"?+?DATE_FORMAT.format(newDate)); ????} ????/** ?????*?在给定的日期加上或减去指定月份后的日期 ?????* ?????*?@param?sourceDate?原始时间 ?????*?@param?month??????要调整的月份,向前为负数,向后为正数 ?????*?@return ?????*/ ????public?static?Date?stepMonth(Date?sourceDate,?int?month)?{ ????????Calendar?c?=?Calendar.getInstance(); ????????c.setTime(sourceDate); ????????c.add(Calendar.MONTH,?month); ????????return?c.getTime(); ????} }

JAVA取得昨天的当前时间?

JAVA取得昨天的当前时间的方法 1、定义时间格式 private static final String CURRENT_DATE_FORMAT = "yyyy-MM-dd hh24:mm:ss"; 2、定义format方法内容 public final static String format(Date date) { DateFormat dateFormat = new SimpleDateFormat(CURRENT_DATE_FORMAT); return dateFormat.format(date); } 3、获取昨天的时间并format完后输出标准格式 public final static String formatYesterday() { return format(new Date(new Date().getTime() - 24*3600*1000)); //定义date实例后,减去24*3600*1000就默认减了一天。



}

java怎么获取当前系统时间?

展开全部 首先获取当前时间: java.util.Date nowdate = new java.util.Date(); 2/2 然后如果你想时间的格式和你想用的时间格式一致 那么就要格式化时间了SimpleDateFormat 的包在java.text包下SimpleDateFormat? sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") //年月日 时分秒 String t = sdf.parse(nowdate);

野草云提供适合入门建站香港云服务器 年付138元起 3M带宽 2GB内存

野草云服务商在前面的文章中也有多次提到,算是一个国内的小众服务商。促销活动也不是很多,比较专注个人云服务用户业务,之前和站长聊到不少网友选择他们家是用来做网站的。这不看到商家有提供香港云服务器的优惠促销,可选CN2、BGP线路、支持Linux与windows系统,支持故障自动迁移,使用NVMe优化的Ceph集群存储,比较适合建站用户选择使用,最低年付138元 。野草云(原野草主机),公司成立于20...

Hostodo,美国独立日特价优惠,四款特价VPS云服务器7折,KVM虚拟架构,NVMe阵列,1核512M内存1Gbps带宽3T月流量,13.99美元/月,赠送DirectAdmin授权

Hostodo近日发布了美国独立日优惠促销活动,主要推送了四款特价优惠便宜的VPS云服务器产品,基于KVM虚拟架构,NVMe阵列,1Gbps带宽,默认分配一个IPv4+/64 IPv6,采用solusvm管理,赠送收费版DirectAdmin授权,服务有效期内均有效,大致约为7折优惠,独立日活动时间不定,活动机型售罄为止,有需要的朋友可以尝试一下。Hostodo怎么样?Hostodo服务器好不好?...

美国高防云服务器 1核 1G 10M 38元/月 百纵科技

百纵科技:美国云服务器活动重磅来袭,洛杉矶C3机房 带金盾高防,会员后台可自助管理防火墙,添加黑白名单 CC策略开启低中高.CPU全系列E52680v3 DDR4内存 三星固态盘列阵。另有高防清洗!百纵科技官网:https://www.baizon.cn/联系QQ:3005827206美国洛杉矶 CN2 云服务器CPU内存带宽数据盘防御价格活动活动地址1核1G10M10G10G38/月续费同价点击...

java得到当前时间为你推荐
mdmMDM产品是如何获取管理终端的权限的?ioeIOE是什么意思知识库管理系统什么是知识管理foxmail邮箱注册如何注册一个foxmail邮箱拓扑关系什么是空间数据的拓扑关系天融信防火墙如何使用天融信NGFW4000防火墙工具调度系统1.说明高级调度、中级调度和低级调度的基本含义。暴力破解密码8位密码暴力破解要多久药品标准查询药品国家标准怎么查阅菜霸现实中遇到地痞流氓该怎么办
高防服务器租用选锐一 新通用顶级域名 l5639 info域名 debian7 java虚拟主机 上海域名 qingyun 河南m值兑换 网站cdn加速 最好的免费空间 如何注册阿里云邮箱 卡巴斯基免费试用版 腾讯总部在哪 789 监控服务器 php服务器 服务器防火墙 贵阳电信 注册阿里云邮箱 更多