In the project development, many times will use the Android time, lists the acquisition time the way, and everybody studies together progress
first, get the system time
1. To get the current time of the system through the Calendar class
Calendar calendar = Calendar.getinstance ();
Long unixtime = Calendar.gettimeinmillis ();//This is the timestamp
logger.i (TAG, "Calendar--->>>" + "Current time is:" + Calendar.get (calendar.year) +
"year" + Calendar.get (calendar.month) +
"month" + calendar.get (calendar.day_of_month) +
"Day" + calendar.get (calendar.hour_of_day) +
"Time" + calendar.get (calendar.minute) +
"min" + calendar.get (Cal Endar. SECOND) +
"seconds");
Print the results as follows:
Calendar--->>> current time: June 13, 2016 14:38 58 seconds
2. Using time to obtain,the Android document says that use time is less than the CPU performance of the calendar, but I do not recommend time, because time use must be in a 24-hour system, if the user's phone is set to 12-hour , then there is the error
Time times = new Time ("Gmt+8");
/* for DateFormat, which has been set to GMT time Standard,
all the string dates required for him to convert are GMT Standard Time, date
returned after conversion due to default
* Compliance with the system default time zone, so it takes +8 to convert to dates
(for example, the Beijing Standard Time zone), which is the time zone that causes the difference between the standards. * *
Times.settonow ();
int year = Times.year;
int month = Times.month;
int day = Times.monthday;
int minute = Times.minute;
int hour = Times.hour;
int sec = Times.second;
LOGGER.I (TAG, "The current time is:" +
Year + "years" + month +
"month" +
Day + "days" + hour +
"time" + Minute +
"min" + S EC +
"seconds");
Print results: The current time: June 13, 2016 6:38 58 seconds
Note: this time and the above use time is the same time, but the time I have been adjusted to be 12-hour system
Of course, there are other ways to get it, but the results are basically the same.
Import Java.text.SimpleDateFormat;
SimpleDateFormat Formatter = new simpledateformat ("YYYY year mm month DD Day HH:mm:ss");
Date curdate = new Date (System.currenttimemillis ());
String str = Formatter.format (curdate);
And also
DateFormat df = new SimpleDateFormat ("HH:mm:ss");
Df.format (New Date ());
Second, access to network time
Most of the time, because of the uncertainty of the phone, resulting in different phone times are not the same, if the development needs to obtain a unified time to match some of the rules, such as my previous company is through or to the current time and after the conversion to match the rules of the network connection, to prevent a large number of Then this time need a unified time and background to match, this is simple to use to get the system time may be a problem, the user's phone if the automatic access to the network time is no problem, but if not it? So getting online time is useful.
URL url = null;//Gets the resource object
try {
url = new URL ("http://www.baidu.com");
URLConnection UC = url.openconnection ()///Generate Connection Object
Uc.connect ();//Send connection
ld = uc.getdate ()//Get website Date time
LOGGER.I (TAG, "ld---->>>>" +ld);
} catch (Exception e) {
e.printstacktrace ();
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.