Android/java string/date/time stamp conversions

Source: Internet
Author: User
Tags dateformat

The dates used in Android are formatted the same as in Java. The general requirement is to convert the current date or the time stamp from the background into a formatted string, organized as follows:

Convert a string to date.

String test = new String ("2018/03/01");

SimpleDateFormat sdf1 = new SimpleDateFormat ("yyyy/mm/dd/hh point MM");

try {
LOG.E ( "test", Sdf1.parse (Test). toString ());     
} catch (parseexception e) {
E.printstacktrace ();
  LOG.E ( "test", e.tostring ());
}

<  Span style= "COLOR: #000080" > error after operation < Span style= "color: #008000; Font-weight:bold ">java.text.parseexception:unparseable Date: " 2018/ 03/01 ". This operation requires the given string and format to correspond , or the exception will be resolved. Changing the test to "2018/03/01/11/32" will still report the same exception, and no other examples of errors will be cited here. That is, in addition to the number and letter correspondence, the rest of the date and the connector should also correspond to the normal resolution. The parse is a Date object.

Ii. Converting a Date object to a formatted string

Date date = new Date ();

< Span style= "COLOR: #000080" > LOG.E (" Test ", Sdf1.format (date));//E/TEST:2018/03/01/11 40 points

Here's the date. Date is a JDK1.0 class that represents an instance of a time that is accurate to milliseconds, and the default construction method passes the current millisecond value of the system, and System.currenttimemillis () may not be accurate enough to refer to the documentation for leap seconds and gmt/ut for details. You can also pass in the specified millisecond value at construction time. There are several constructs that are discarded after JDK1.1, which are passed in the specified month and day minutes or seconds or strings, and then parse the formatting, but these methods have problems with internationalization, so Java converts the date and time in the calendar and converts the date and string in DateFormat, respectively. Calendar.set (year + 1900, month, date) or gregoriancalendar ( Year + 1900, month, date); dateformat.parse (String s)

< Span style= "COLOR: #000080" > three, TimeStamp

< Span style= "COLOR: #000080" > TimeStamp extends Java.util.Date, the constructor passed in a millisecond value. Because it is a subclass of date, it can also be formatted as a specified string by DateFormat. Date can also call gettime () to get the millisecond value. Timestamp has a static method valueof (String str) can convert a string to timestamp object, but this method requires a string in the specified format.

Another problem in the project is that the timestamp in Java, Syste.currenttimemillis () returns 13-bit long data, while the service-side is generally 10-bit timestamp, paying attention to the problem of transformation.

Android/java string/date/time stamp conversions

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.