In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to obtain the current system time in Java". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to get the current system time in Java" can help you solve the problem.
Commonly used time class-Date class
The use of the time class is very simple and can be used by instantiating the Date object. If you do not pass parameters, the instantiated object represents the time of the current system.
The year, month, day, hour, and week of the Date object can be obtained through the get method, but the data needs to be processed in the case of the year and month (the year is based on 1900 and the month begins in October).
Using the set method, you can modify the year, month, day, hour, and week of this Date object, but in the case of year and month, you need to process the data before setting it (ibid.).
Import java.util.Date;public class timeDate {public static void main (String [] args) {Date D1 = new Date (); / / create a Date object System.out.println (D1) that represents the current date of the system; int year = d1.getYear () + 1900 / / you can use the get method to get the year, month, day, hour, second and week of this Date object / / add 1900 to get the exact year System.out.println (year); int mouth = d1.getMonth () + 1; / / you need + 1 to get the exact month System.out.println (mouth) when you get the month; d1.setYear (2020-1900) / / you can use the set method to set the year, month, day, hour, second and week of this Date object / / subtract 1900 when setting the year to get the exact year / / you need-1 to set the exact month System.out.println (D1) when setting the month;}}
A better time class-Calender class
Due to insufficient design, many methods of the Date class have been deprecated since java 1.1. At this point, you can also consider a better time class, the Calender class, which acquires time differently from the Date class. The code is as follows, which can be compared by reference.
Import java.util.Calendar;public class timeCalender {public static void main (String [] args) {Calendar D1 = Calendar.getInstance (); / / create a Calender object int year = d1.get (Calendar.YEAR) that represents the current date of the system; / / get the year int mouth = d1.get (Calendar.MONTH) + 1 / / get month (need + 1) int day = d1.get (Calendar.DAY_OF_MONTH); / / get date int hours = d1.get (Calendar.HOUR_OF_DAY); / / get hour int minutes = d1.get (Calendar.MINUTE); / / get minute int seconds = d1.get (Calendar.SECOND) / / get second System.out.println (year+ "-" + mouth+ "-" + day+ "-" + hours+ ":" + minutes+ ":" + seconds);}} that's all for "how to get the current system time in Java". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.