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

BuyVM迈阿密KVM上线,AMD Ryzen 3900X+NVMe硬盘$2/月起

BuyVM在昨天宣布上线了第四个数据中心产品:迈阿密,基于KVM架构的VPS主机,采用AMD Ryzen 3900X CPU,DDR4内存,NVMe硬盘,1Gbps带宽,不限制流量方式,最低$2/月起,支持Linux或者Windows操作系统。这是一家成立于2010年的国外主机商,提供基于KVM架构的VPS产品,数据中心除了新上的迈阿密外还包括美国拉斯维加斯、新泽西和卢森堡等,主机均为1Gbps带...

Virmach$7.2/年,新款月抛vps上线,$3.23/半年,/1核640M内存/10 GB存储/ 1Gbps/1T流量

Virmach自上次推出了短租30天的VPS后,也就是月抛型vps,到期不能续费,直接终止服务。此次又推出为期6个月的月抛VPS,可选圣何塞和水牛城机房,适合短期有需求的用户,有兴趣的可以关注一下。VirMach是一家创办于2014年的美国商家,支持支付宝、PayPal等方式,是一家主营廉价便宜VPS服务器的品牌,隶属于Virtual Machine Solutions LLC旗下!在廉价便宜美国...

CloudCone,美国洛杉矶独立服务器特价优惠,美国洛杉矶MC机房,100Mbps带宽不限流量,可选G口,E3-1270 v2处理器32G内存1Gbps带宽,69美元/月

今天CloudCone发布了最新的消息,推送了几款特价独立服务器/杜甫产品,美国洛杉矶MC机房,分配100Mbps带宽不限流量,可以选择G口限制流量计划方案,存储分配的比较大,选择HDD硬盘的话2TB起,MC机房到大陆地区线路还不错,有需要美国特价独立服务器的朋友可以关注一下。CloudCone怎么样?CloudCone服务器好不好?CloudCone值不值得购买?CloudCone是一家成立于2...

java得到当前时间为你推荐
mdm中国移动mdm是干什么的weakhashmapjava中几种Map在什么情况下使用,并简单介绍原因及原理空白代码html空格代码怎么写spawningVC中Error spawning cl.exe错误的解决方法.qq业务中心QQ业务办理数据分析报告范文如何写数据分析报告?菜霸保险是传销吗?基础设施即服务基础设施行业的定义是什么?具体包含哪些行业?建立qq号QQ号码怎么建?酷源码怎样看源码下优酷视频
网通vps linuxvps 抗投诉vps主机 如何注册网站域名 bbr 美国翻墙 pccw 香港cdn 流媒体服务器 wdcp 京东云擎 mysql主机 免费个人网站申请 免费防火墙 umax120 网购分享 免费的asp空间 韩国代理ip 双线空间 攻击服务器 更多