Android common system time acquisition

Source: Internet
Author: User
Tags add time dateformat

1. Calendar

Calendar C = calendar.getinstance ();

Get system date: Year = C.get (calendar.year)

month = C.GRT (calendar.month)

Day = C.get (calendar.day_of_month)

Acquisition system time: Hour = C.get (Calendar.hour_of_day);

minute = C.get (Calendar.minute)

2. New Date

SimpleDateFormat formatter = new SimpleDateFormat ("yyyy mm month DD Day HH:mm:ss");

Date curdate = new Date (System.currenttimemillis ());//Get current time

String str = Formatter.format (curdate);

Time zone can be specified (pending):
Df=dateformat.getdatetimeinstance (Dateformat.full,dateformat.full,locale.china);

System.out.println (Df.format (New Date ()));

How to get the Android system time is 24 hour or 12 hour system
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, new time get 24 hours

Time T=new time (); or time T=new time ("gmt+8"); Add time zone information.

T.settonow (); Get 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;

Know:

1. Oracle default system time is the Sysdate function, the stored data shape such as 25-3-200510:55:33
2. The time-fetching object in Java 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 java.util.Date subclasses.
4. The most significant relationship between Oracle and date operations is two conversion functions: To_date (), To_char (). To_date () is typically used to write dates to a database using a function. To_char () is typically used to read from a database into a date function.

DATE, Time, and TIMESTAMP:
SQL defines three time-related data types: date is made up of day, month, and year. Time is made up of hours, minutes, and seconds. TIMESTAMP combines DATE with time and adds the nanosecond domain.
The standard Java class Java.util.Date can provide date and time information. However, because the class contains DATE and time information and does not have the nanosecond required for TIMESTAMP, it does not exactly match the above three types of SQL.
So we have defined the three seed classes of java.util.Date. They are:
1. Java.sql.Date for SQL DATE information
2. Java.sql.Time for SQL time information
3. Java.sql.Timestamp about SQL TIMESTAMP information
For the Java.sql.time,java.util.time base class, the hour, minute, second, and millisecond fields are set to zero. For the Java.sql.date,java.util.date base class, the year, month, and day fields are set to 1970 years, respectively, 1

1st of the month. This is the "0" date in the Java era. The dates in java.sql.date can be compared to fields with dates in standard SQL statements. Java.sql.Timestamp class expands by adding a nanosecond domain

Java.util.Date.

Two conversion functions in Oracle:
1. The to_date () function converts a character type to a date type in a certain format:
Specific usage: to_date (' 2004-11-27 ', ' yyyy-mm-dd '), the former is a string, the latter is a conversion date format, note that before and after the two to a corresponding. such as; To_date (' 2004-11-27 13:34:43 ', ' yyyy-mm-dd

Hh24:mi:ss ") will be given a specific time.
2. To_char (): convert date to a certain format for character type:
Specific usage: to_char (sysdate, "Yyyy-mm-dd hh24:mi:ss")

To_date () with 24-hour notation and MM-minute display:
When using Oracle's To_date function for date conversion, many Java programmers may directly use the format "Yyyy-mm-dd HH:mm:ss" as a format for conversion, but will cause an error in Oracle: "ORA 01810

The format code appears two times.
such as: Select To_date (' 2005-01-01 13:14:20 ', ' yyyy-mm-dd HH24:mm:ss ') from dual;
The reason is that SQL is case-insensitive, mm and mm are considered the same format code, so Oracle's SQL uses MI instead of minutes. Oracle's default system time is the Sysdate function, and the stored data is shaped like 2005-3-2510:55:33

, the time-fetching object in Java is java.util.Date.
Select To_date (' 2005-01-01 13:14:20 ', ' yyyy-mm-dd HH24:mi:ss ') from dual

Examples of operations on a date field in Java Operations with Oracle:
Table book has name VARCHAR2 (20)//books name, Buydate date//Purchase date two fields.
A database connection has been created connection conn;

Method one, using java.sql.Date to achieve a relatively simple YYYY-MM-DD format date. Java.sql.Date does not support the time format. Remember not to use the new java.sql.Date (int year,int month,int Date) because you also want to place

Time lag problem.
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.execute ();
Method Two, use Java.sql.Timestamp, ditto 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.execute ();
Method three, using Oracle's to_date built-in functions
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.execute ();
Attached: Oracle date format parameter meaning description
D: The week of the week
Day: The name of the days, padded with spaces to 9 characters
DD: The day ordinal of the month
DDD: The day ordinal of the year
DY: Abbreviated name of the day
Iw:iso the first week of the year in the standard
Iyyy:iso standard four-bit year
YYYY: four-bit year
Yyy,yy,y: Last three digits of the year, two digits, one
HH: Hours, by 12 hours
Hh24: Hours, by 24 hours
MI: Min
SS: Seconds
MM: Month
Mon: Shorthand for the month
Month: Full name of the month
W: The first few weeks of the month
WW: The first few weeks of the year



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.