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

LightNode($7.71/月)香港cn2精品线路

LightNode官网LightNode是一家位于香港的VPS服务商.提供基于KVM虚拟化技术的VPS.在提供全球常见节点的同时,还具备东南亚地区、中国香港等边缘节点.满足开发者建站,游戏应用,外贸电商等应用场景的需求。为用户带来高性能服务器以及优质的服务的同时还提供丰厚的促销活动,新用户注册最高送$20。注册用户带新客即可得10%返佣。商家支持PayPal,支付宝等支付方式。官网:https:/...

趣米云月付460元,香港CN2云服务器VPS月付低至18元

趣米云早期为做技术起家,为3家IDC提供技术服务2年多,目前商家在售的服务有香港vps、香港独立服务器、香港站群服务器等,线路方面都是目前最优质的CN2,直连大陆,延时非常低,适合做站,目前商家正在做七月优惠活动,VPS低至18元,价格算是比较便宜的了。趣米云vps优惠套餐:KVM虚拟架构,香港沙田机房,线路采用三网(电信,联通,移动)回程电信cn2、cn2 gia优质网络,延迟低,速度快。自行封...

racknerd:美国大硬盘服务器(双路e5-2640v2/64g内存/256gSSD+160T SAS)$389/月

racknerd在促销美国洛杉矶multacom数据中心的一款大硬盘服务器,用来做存储、数据备份等是非常划算的,而且线路还是针对亚洲有特别优化处理的。双路e5+64G内存,配一个256G的SSD做系统盘,160T SAS做数据盘,200T流量每个月,1Gbps带宽,5个IPv4,这一切才389美元...洛杉矶大硬盘服务器CPU:2 * e5-2640v2内存:64G(可扩展至128G,+$64)硬...

java得到当前时间为你推荐
ioeucl-ioe的学位证到底是ucl?还是后面加了一个ioe诺诺云代账上海的亮证节有讲到诺诺云代账,产品如何?开票系统怎样开普通发票系统附清单weakhashmapjava中几种Map在什么情况下使用,并简单介绍原因及原理weakhashmap在Java中ArrayList、LinkedList、HashMap的区别是什么md5值游戏安装包的MD5值怎么用?溢出隐藏overflow:hidden:溢出隐藏了。jdk6java—JDK6,在SUN公司官网下载的链接,调度系统操作系统中为什么需要调度?索引超出了数组界限什么是索引超出了数组界限
域名备案中心 google镜像 云网数据 simcentric pccw cdn服务器 163网 512m dropbox网盘 debian7 100m免费空间 谁的qq空间最好看 免费智能解析 申请网页 银盘服务是什么 国外在线代理服务器 华为云建站 网站加速 国内空间 windows2008 更多