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

lcloud零云:沪港IPLC,70元/月/200Mbps端口/共享IPv4/KVM;成都/德阳/雅安独立服务器低至400元/月起

lcloud怎么样?lcloud零云,UOVZ新开的子站,现在沪港iplc KVM VPS有端午节优惠,年付双倍流量,200Mbps带宽,性价比高。100Mbps带宽,500GB月流量,10个,512MB内存,优惠后月付70元,年付700元。另有国内独立服务器租用,泉州、佛山、成都、德阳、雅安独立服务器低至400元/月起!点击进入:lcloud官方网站地址lcloud零云优惠码:优惠码:bMVbR...

限时新网有提供5+个免费域名

有在六月份的时候也有分享过新网域名注册商发布的域名促销活动(这里)。这不在九月份发布秋季域名促销活动,有提供年付16元的.COM域名,同时还有5个+的特殊后缀的域名是免费的。对于新网服务商是曾经非常老牌的域名注册商,早年也是有在他们家注册域名的。我们可以看到,如果有针对新用户的可以领到16元的.COM域名。包括还有首年免费的.XYZ、.SHOP、Space等等后缀的域名。除了.COM域名之外的其他...

Ceranetworks顶级合作伙伴 香港E3 16G 299元 香港E5 32G 650元 美国E3 16G 650元

提速啦(www.tisula.com)是赣州王成璟网络科技有限公司旗下云服务器品牌,目前拥有在籍员工40人左右,社保在籍员工30人+,是正规的国内拥有IDC ICP ISP CDN 云牌照资质商家,2018-2021年连续4年获得CTG机房顶级金牌代理商荣誉 2021年赣州市于都县创业大赛三等奖,2020年于都电子商务示范企业,2021年于都县电子商务融合推广大使。资源优势介绍:Ceranetwo...

java得到当前时间为你推荐
ISDNISDN是什么网络?rbf神经网络RBF神经网络和BP神经网络有什么区别空白代码html空格代码怎么写拓扑关系简述空间数据的拓扑关系及其对GIS数据处理和空间分析有何重要意义?slideshare什么是slide sandalsmartuploadjspsmartupload如何使用?相似图片搜索怎么找手机上的一张相似图片?文件系统类型文件系统的类型是 NTFS。无法决定卷版本和状态。CHKDSK 被终止?toolstripWinOperationClass是什么来的,什么用建立qq号QQ号码怎么建?
cybermonday linode日本 themeforest kddi mobaxterm 国外免费全能空间 183是联通还是移动 cdn加速是什么 息壤代理 gtt 银盘服务是什么 卡巴斯基官网下载 云销售系统 免费赚q币 winserver2008r2 paypal登陆 neicun dbank 瓦工技术 qq空间技术网 更多