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

云如故枣庄高防(49元)大内存2H2G49元8H8G109元

云如故是一家成立于2018年的国内企业IDC服务商,由山东云如故网络科技有限公司运营,IDC ICP ISP CDN VPN IRCS等证件齐全!合法运营销售,主要从事自营高防独立服务器、物理机、VPS、云服务器,虚拟主机等产品销售,适合高防稳定等需求的用户,可用于建站、游戏、商城、steam、APP、小程序、软件、资料存储等等各种个人及企业级用途。机房可封UDP 海外 支持策略定制 双层硬件(傲...

数脉科技香港自营,10Mbps CN2物理机420元/月

数脉科技怎么样?数脉科技品牌创办于2019,由一家从2012年开始从事idc行业的商家创办,目前主营产品是香港服务器,线路有阿里云线路和自营CN2线路,均为中国大陆直连带宽,适合建站及运行各种负载较高的项目,同时支持人民币、台币、美元等结算,提供支付宝、微信、PayPal付款方式。本次数脉科技给发来了新的7月促销活动,CN2+BGP线路的香港服务器,带宽10m起,配置E3-16G-30M-3IP,...

618云上Go:腾讯云秒杀云服务器95元/年起,1C2G5M三年仅288元起

进入6月,各大网络平台都开启了618促销,腾讯云目前也正在开展618云上Go活动,上海/北京/广州/成都/香港/新加坡/硅谷等多个地区云服务器及轻量服务器秒杀,最低年付95元起,参与活动的产品还包括短信包、CDN流量包、MySQL数据库、云存储(标准存储)、直播/点播流量包等等,本轮秒杀活动每天5场,一直持续到7月中旬,感兴趣的朋友可以关注本页。活动页面:https://cloud.tencent...

java得到当前时间为你推荐
g2g吉他的效果器的名称是什么意思?(G1G,G2G等等)foxmail邮箱注册FOXMAIL邮箱在哪里可以注册?云图片云相册是什么意思电子日历墙上挂的电子日历不显示怎么维修李昊天铠甲勇士刑天中人物资料deviceid怎么能知道安卓系统手机的DEVICE ID?数秦科技奇秦科技是做什么的,大家了解过吗?qq业务中心QQ业务办理screencapture电脑qq问题:点击qq邮箱与空间,弹出Screen Capture Control 进入不了qq邮箱与空间,怎么解决?screencapture求一款这样的截图软件
美国域名注册 成都主机租用 免费域名跳转 双11抢红包攻略 线路工具 ev证书 华为4核 小米数据库 炎黄盛世 hostker 服务器监测 网购分享 万网服务器 闪讯网 小夜博客 nnt 上海联通 美国主机侦探 空间排行榜 美国asp空间 更多