Android gets the timestamp and converts the timestamp to the time

Source: Internet
Author: User

Android gets the timestamp and converts the timestamp to the time

In actual development, the time returned by the server is generally in the timestamp format. In this case, the client needs to process the timestamp and convert the timestamp to the standard time format.

Print:


Directly run the Code: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PHByZSBjbGFzcz0 = "brush: java;"> package com. androidtimestampdemo; import java. text. simpleDateFormat; import java. util. calendar; import java. util. date; import java. util. locale; import android. app. activity; import android. OS. bundle; import android. util. log; public class MainActivity extends Activity {private long timecurrentTimeMillis; private long timeGetTime; private long timeSeconds; private long time Millis; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); init ();} private void init () {/*** The following methods are used to obtain the current system timestamp, in actual development, the server may need to return the required timestamp; * in actual development, the timestamp returned by the server may be a string and must be converted to long. The following method can be used for direct conversion: * Integer. parseInt (String s) Long. parseLong (String s) * Float. parseFloat (String s) Double. parseDouble (String s) */timecurrentTimeMi Llis = System. currentTimeMillis (); timeGetTime = new Date (). getTime (); timeSeconds = System. currentTimeMillis (); timeMillis = Calendar. getInstance (). getTimeInMillis ();/*** you can see through the print information that the timestamps of these types of acquisition systems are almost the same. */Log. d ("zhongyao", "timer" + timecurrentTimeMillis + "@" + "timeGetTime" + timeGetTime + "@ timeSeconds" + timeSeconds + "timeMillis" + timeMillis ); /*** time stamp converted to specific time format */SimpleDateFormat sdf = new SimpleDateFormat ("yyyy MM dd", Locale. getDefault (); String time1 = sdf. format (timeSeconds); String time2 = sdf. format (timeGetTime); Log. d ("zhongyao", timeSeconds + "current time 1->:" + time1); Log. d ("zhongyao", timeGetTime + "current time 2 -->:" + time2 ); simpleDateFormat sdfTwo = new SimpleDateFormat ("MM mm dd, yyyy, hh mm mm ss seconds E", Locale. getDefault (); String time11 = sdfTwo. format (timeSeconds); String time22 = sdfTwo. format (timeGetTime); Log. d ("zhongyao", timeSeconds + "current time 11->:" + time11); Log. d ("zhongyao", timeGetTime + "current time 22 -->:" + time22);/*** most commonly used: * because the server returns a UNIX timestamp, therefore, you need to convert the UNIX timeStamp to a fixed format String */String result = formatData ("yyyy-MM-dd", 1414994617); Log. d ("zhongyao", result);} public static String formatData (String dataFormat, long timeStamp) {if (timeStamp = 0) {return "";} timeStamp = timeStamp * 1000; String result = ""; SimpleDateFormat format = new SimpleDateFormat (dataFormat); result = format. format (new Date (timeStamp); return result ;}}



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.