Android gets UTC time and conversion to. NET Timestamps

Source: Internet
Author: User
Tags epoch time set time

Tag: UTC time Timestamp

This article is purely integrated and records the UTC time used in the project and the conversion to. NET timestamps.

1, Android get UTC time

/**

* Get UTC time

*

* @return

*/

public static String Getutctimestr () {

DateFormat format = new SimpleDateFormat ("Yyyy/mm/dd/hh/mm/ss");

StringBuffer utctimebuffer = new StringBuffer ();

1. Get local time:

Calendar cal = Calendar.getinstance ();

2. Get time Offset:

int zoneoffset = Cal.get (Java.util.Calendar.ZONE_OFFSET);

3, to obtain daylight saving time difference:

int dstoffset = Cal.get (Java.util.Calendar.DST_OFFSET);

4. Deduct these differences from local time, that is, you can get UTC time:

Cal.add (Java.util.Calendar.MILLISECOND,-(Zoneoffset + dstoffset));

int year = Cal.get (calendar.year);

int month = Cal.get (calendar.month) + 1;

int day = Cal.get (calendar.day_of_month);

int hour = Cal.get (Calendar.hour_of_day);

int minute = Cal.get (Calendar.minute);

int second = Cal.get (Calendar.second);

Utctimebuffer.append (year). Append ("/"). Append (Month). Append ("/")

. append (day);

Utctimebuffer.append ("/"). Append (Hour). Append ("/"). Append (minute)

. Append ("/"). Append (second);

try {

Format.parse (Utctimebuffer.tostring ());

return utctimebuffer.tostring ();

} catch (ParseException e) {

E.printstacktrace ();

} catch (Java.text.ParseException e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

return null;

}

Just copy it and use it right away.

2. Get time stamp

/**

* Get time stamp

*

* @param datecur

* @return

*/

public static long Getticks (String datecur) {

Convert the Target-epoch time to a Well-format string

String date = new Java.text.SimpleDateFormat ("Yyyy/mm/dd/hh/mm/ss")

. Format (New Date (Long.parselong (EPOCHSTR)));

SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy/mm/dd/hh/mm/ss");

String datecur = Sdf.format (New Date ());

String[] ds = Datecur.split ("/");


Start of the Ticks time

Calendar Calstart = Calendar.getinstance ();

/**

* The parameters here are very important, in principle, is 1, the date is 2, because the previous date did not lose 1 the third parameter is 1: the date is more than 2 days, 2 is the date more than 1 days

* **/

When the upload fails, there will always be confusion and need to find the source to solve

Calstart.set (1, 1, 0, 0, 0, 0);

Calstart.set (1, 1, 3, 0, 0, 0);


The target time

Calendar calend = Calendar.getinstance ();

Calend.set (Integer.parseint (Ds[0]), Integer.parseint (Ds[1]),

Integer.parseint (ds[2]), Integer.parseint (Ds[3]),

Integer.parseint (Ds[4]), Integer.parseint (Ds[5]);


Epoch time of the Ticks-start time

Long Epochstart = Calstart.gettime (). GetTime ();

Epoch time of the target time

Long epochend = Calend.gettime (). GetTime ();


Get the sum of epoch time, from the target time to the Ticks-start

Time

Long all = Epochend-epochstart;

Convert epoch time to ticks time

Long ticks = ((all/1000) * 1000000) * 10;


return ticks;

}

Pass the UTC time obtained in the first step to the second step to get the timestamp!


For the interpretation of timestamps, I will cite an article to illustrate that individuals are actually exploring:

Java Date.gettime () converted to C # datetime.ticks

First, a noun to explain:
Epoch Time: The number of second (seconds) since 0 o'clock on January 1, 1970.
Note that I quoted "second (seconds)" because in different projects, the units of measure may differ, and you need to read the relevant documentation carefully. For example, in the Gmail notifications documentation for Gtalk API, The number of date used is "millisecond (milliseconds)" up to now from 0 o'clock January 1, 1970.
C # Datetime.ticks: Refers to the number of one ten-millionth of a second up to now from 0 o'clock January 1, 01, or one hundred nanoseconds of a second, i.e. " One out of 10,000 seconds "of the number.
Java date.gettime (): This method returns the number of "millisecond (milliseconds)" for the target time until 0 o'clock January 1, 1970.

And then we'll do a conversion:
1 second (SEC) =1000 millisecond (msec) =10 x 0000 One ten-millionth of a second (one out of 10,000 seconds)

Okay, here's our Java conversion function.

public static long Getticks (String epochstr)
{
Convert the Target-epoch time to a Well-format string
String date = new Java.text.SimpleDateFormat ("Yyyy/mm/dd/hh/mm/ss"). Format (new Date (Long.parselong (EPOCHSTR)));
String[] Ds=date.split ("/");

Start of the Ticks time
Calendar calstart=calendar.getinstance ();
Calstart.set (1, 1, 3, 0, 0, 0);

The target time
Calendar calend=calendar.getinstance ();
Calend.set (Integer.parseint (Ds[0]), Integer.parseint (Ds[1]), Integer.parseint (Ds[2]), Integer.parseint (Ds[3]), Integer.parseint (Ds[4]), Integer.parseint (Ds[5]);

Epoch time of the Ticks-start time
Long Epochstart=calstart.gettime (). GetTime ();
Epoch time of the target time
Long Epochend=calend.gettime (). GetTime ();

Get the sum of epoch time, from the target time to the Ticks-start time
Long All=epochend-epochstart;
Convert epoch time to ticks time
Long ticks= ((all/1000) * 1000000) * 10;

return ticks;
}

To illustrate with a diagram:

|         | |
Target time 1970 0001 years

I took the target time separately from 01 to 1970 "Millisecond (milliseconds)", then added together, so that the target time to 01 "millisecond (milliseconds)", and then convert this number to "one out of 10,000 seconds" of the number, Get the number of ticks.
Perhaps you will find out why the 0001 calculation from January 3 onwards, is not supposed to be January 1. This question I am also very strange, because I found that if from January 1, time is always bad for two days, this reason waits for the master to solve:)

Note: the. NET is really starting from January 01, 01. But historically because of the conversion of the calendar, there are "lost 2 days."

individuals found a problem in the project, Calstart.set (1, 1, 3, 0, 0, 0); When the settings here will change at different times, resulting in the last set time also changes, some systems are calculated from 1970/01/01 , some will start from 1970/01/02 , resulting in the time I get inconsistent, Every time the problem will need to change the settings here (Calstart.set (1, 1, 3, 0, 0, 0)) to return to normal, if a friend also occurs such problems, please share, I will not very grateful, I if the study came out also will update to share, thank you! Welcome to Explore!

This article is from the "Suck on self-renewal" blog, please be sure to keep this source http://wyong.blog.51cto.com/1115465/1617679

Android gets UTC time and conversion to. NET timestamp

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.