Acquisition of common Android system time

Source: Internet
Author: User

Acquisition of common Android system time

1. Calendar

Calendar c = Calendar. getInstance ();

Obtain the system date: year = c. get (Calendar. YEAR)

Month = c. grt (Calendar. MONTH)

Day = c. get (Calendar. DAY_OF_MONTH)

Obtain the system time: hour = c. get (Calendar. HOUR_OF_DAY );

Minute = c. get (Calendar. MINUTE)

2. new Date

SimpleDateFormat formatter = new SimpleDateFormat ("MM dd, yyyy HH: mm: ss ");

Date curDate = new Date (System. currentTimeMillis (); // obtain the current time

String str = formatter. format (curDate );

You can specify the time zone (to be ):
Df = DateFormat. getDateTimeInstance (DateFormat. FULL, DateFormat. FULL, Locale. CHINA );

System. out. println (df. format (new Date ()));

How to obtain whether the Android system is in the 24-hour or 12-hour format
ContentResolver cv = this. getContentResolver ();

String strTimeFormat = android. provider. Settings. System. getString (cv,

Android. provider. Settings. System. TIME_12_24 );

(StrTimeFormat. equals ("24 "))

{Log. I ("activity", "24 ");}

3. Get the 24-hour Time for the new Time

Time t = new Time (); // or Time t = new Time ("GMT + 8"); plus the Time Zone information.

T. setToNow (); // obtain the system time.

Int year = t. year;

Int month = t. month;

Date = t. monthDay;

Int hour = t. hour; // 0-23

Int minute = t. minute;

Int second = t. second;

Understanding:

1. the default system time of oracle is the sysdate function. The stored data is like 25-3-200510: 55: 33.
2. in java, the time object is java. util. Date.
3. The corresponding Time objects in oracle are java. util. Date, java. SQL. Time, java. SQL. Timestamp, and they are all subclasses of java. util. Date.
4. The biggest relationship between oracle and date operations is two conversion functions: to_date () and to_char (). To_date () is generally used to write data to the database. To_char () is generally used to read data from a database.

DATE, TIME, and TIMESTAMP:
SQL defines three time-related data types: DATE is composed of days, months, and years. TIME consists of hours, minutes, and seconds. TIMESTAMP combines DATE and TIME, and adds a nanosecond field.
Standard Java. util. Date provides Date and time information. However, because the class contains DATE and TIME information and does not have the nanoseconds required by TIMESTAMP, it does not match the preceding three SQL types.
Therefore, we define three subclasses of java. util. Date. They are:
1. java. SQL. DATE
2. java. SQL. TIME for SQL Time information
3. java. SQL. TIMESTAMP
For java. SQL. Time, the hour, minute, second, and millisecond fields of the basic class of java. util. Time are set to zero. The year, month, and day fields of the basic classes of java. SQL. Date and java. util. Date are set to July 1, 1970, respectively.

The first day of the month. This is the "zero" date in the new Java era. The date in java. SQL. date can be compared with the field containing the date in the standard SQL statement. The java. SQL. Timestamp class is extended by adding a nanosecond field.

Java. util. Date.

Two conversion functions in oracle:
1. to_date () converts the character type to the date type in a certain format:
Usage: to_date ('1970-11-27 '', ''yyyy-mm-dd''). The former is a string, and the latter is a conversion date format. Note, the two must be one-to-one. For example, to_date (''2004-11-27 13:34:43'', ''yyyy-mm-dd

Hh24: mi: s.
2. to_char (): Convert the date to a certain format to the character type:
Usage: to_char (sysdate, ''yyyy-mm-dd hh24: mi: s '')

To_date () and 24-hour notation and display of mm minutes:
When Oracle's to_date function is used for date conversion, many Java programmers may directly use the format "yyyy-MM-dd HH: mm: ss" for conversion, however, an error occurs in Oracle: "ORA 01810

The format code appears twice ".
For example, select to_date ('1970-01-01 13:14:20 ', 'yyyy-MM-dd HH24: mm: ss') from dual;
The reason is that SQL statements are case-insensitive. MM and mm are considered to be the same format code, so Oracle SQL uses mi instead of minutes. The default system time of oracle is the sysdate function. The stored data is like 2005-3-2510: 55: 33.

In java, the time object is java. util. Date.
Select to_date ('1970-01-01 13:14:20 ', 'yyyy-MM-dd HH24: mi: ss') from dual

An example of Date Field Operations in oracle operations in java:
The table book contains two fields: name varchar2 (20) // book name and buydate Date // purchase Date.
You have created a database Connection conn;

Method 1: use java. SQL. Date to implement a relatively simple Date in yyyy-mm-dd format. Java. SQL. Date does not support the time format. Do not use new java. SQL. Date (int year, int month, int date) because

Time Difference.
PreparedStatement pstmt = conn. prepareStatement ("insert into book (name, buydate) values (?,?) ");
Java. SQL. Date buydate = java. SQL. Date. valueOf ("2005-06-08 ");
Pstmt. setString (1, "Java programming ideas ");
Pstmt. setDate (2, buydate );
Pstmt.exe cute ();
Method 2: Use java. SQL. Timestamp. Do not use new Timestamp (....)
PreparedStatement pstmt = conn. prepareStatement ("insert into book (name, buydate) values (?,?) ");
Java. SQL. Timestamp buydate = java. SQL. Timestamp. valueOf ("2004-06-08 05: 33: 99 ");
Pstmt. setString (1, "Java programming ideas ");
Pstmt. setTimestamp (2, buydate );
Pstmt.exe cute ();
Method 3: Use the oracle to_date built-in Function
PreparedStatement pstmt = conn. prepareStatement ("insert into book (name, buydate) values (?, To_date (?, 'Yyyy-mm-dd hh24: mi: ss ')");
String buydate = "2004-06-08 05: 33: 99 ";
Pstmt. setString (1, "Java programming ideas ");
Pstmt. setString (2, buydate );
Pstmt.exe cute ();
Appendix: Description of oracle date format parameters
D: day of the week
Day: the name of the day, which is filled with spaces to 9 characters.
Dd: The day of the month
Ddd: The day of the year
Dy: short name of day
Iw: week of the year of the ISO standard
Iyyy: ISO standard four-digit year
Yyyy: four-digit year
Yyy, yy, y: the last three digits of the year, two digits, one digit
Hh: hour, in 12 hours
Hh24: hour, in 24 hours
Mi: minute
Ss: seconds
Mm: Month
Mon: abbreviated month
Month: The full name of the month.
W: The week of the month
Ww: the week of the year



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.