Java Date format Application __ programming language

Source: Internet
Author: User
/** * Obtains a specific time from the number of seconds, 24-hour system, such as 2016-12-12 23:23:15 * @param second * @return/public static String Second2moment24 (long s
    Econd) {Date date = new Date (second*1000);
    SimpleDateFormat ft = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
    Ft.settimezone (Timezone.gettimezone ("gmt+8"));
return Ft.format (date); /** * To obtain a specific time from the number of seconds, 12-hour system, such as 2016-12-12 23:23:15 * @param second * @return/public static String second2moment12 (lon
    G second) {Date date = new Date (second*1000);
    SimpleDateFormat ft = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
    Ft.settimezone (Timezone.gettimezone ("gmt+8"));
return Ft.format (date); /** * Obtain a specific time from the number of seconds, 24-hour system, such as 2016-12-12 23:23:15 * @param second * @return/public static String Second2momentgmt0 (l
    Ong second) {Date date = new Date (second*1000);
    SimpleDateFormat ft = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
    Ft.settimezone (Timezone.gettimezone ("gmt+0")); return Ft.format (date);
/** * from a specific time, such as 2016-12-12 23:23:15, get the number of seconds * @param time * @return/public static long Gettimestamp (String time) {
    SimpleDateFormat SimpleDateFormat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
    Simpledateformat.settimezone (Timezone.gettimezone ("gmt+8"));
    Date date = null;
    try {date = Simpledateformat.parse (time);
    catch (ParseException e) {return 0;
    Long timestemp = Date.gettime ()/1000;
return timestemp;
 }

1, why the conversion from System.currenttimemillis () to standard time format will be 12 hours. For example, the current time is 2016-12-19 21:36:20, call second2moment12 (System.currenttimemillis ()/1000), the output is 2016-12-19 09:36:20. The reason is mixed 12-hour system and 24-hour system, SimpleDateFormat should use " YYYY-MM-DD HH:mm:ss"
2, why the conversion from System.currenttimemillis () to standard time format will be 8 hours. For example, the current time is 2016-12-19 21:36:20, call Second2momentgmt0 (System.currenttimemillis ()/1000), the output is 2016-12-19 13:36:20.
Using System.currenttimemillis () to obtain the system time is the international Standard Time, is 0 time zone times. The reason may be that SimpleDateFormat did not set a time zone, Ft.settimezone ( Timezone.gettimezone ("gmt+8"))。
3, how from a time string such as 2016-12-19 13:36:20 to obtain its seconds, can be used to compare the time of the morning and evening, such as the current time is 2016-12-19 21:36:20, call Gettimestamp ("2016-12-19 21:36:20" ), the output is 1482154580.

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.