Detailed java and javascript date

** java, js date conversion: ** <Excerpt in index | Home summary>
the Java variety of date conversion

<The rest of contents | remaining full-text>

Date represents the type of

  1. Gets the type of long date format

    1
    2
    3
    4
    long time = System.currentTimeMillis();
    System.out.printf(time+"");
    Date date =new Date();
    System.out.println(date.getTime());
  2. Get the date formats developed

    1
    2
    3
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    Date date =new Date();
    System.out.println(sdf.format(date) );
  3. The development of the date format into date or milliseconds

    1
    2
    3
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    Date date = sdf.parse("2016-05-22 10:15:21");
    long mills = date.getTime();
  • Description: System.currentTimeMillis () can not be accurate to 1ms level, it depends on the system is running, you then windows, mac, linux has a precise range differences, the requirements for high precision time, you can not use this

Date Calculations

  1. The most convenient way is to calculate the time value in milliseconds into
    1
    2
    3
    4
    Date from =new Date();
    Thread.sleep(200);
    Date to =new Date();
    System.out.println(to.getTime()-from.getTime());

Precision Time

1
2
long time1 =System.nanoTime();
System.out.printf(time1+"");
  • Description: System.nanoTime () ns level precision improved, 1ms = 1000000ns,

javascript date

  1. Obtaining milliseconds time to get the month, time

    . 1 
    2
    . 3
    . 4
    . 5
    . 6
    . 7
    . 8
    . 9
    10
    . 11
    large columns   java and javascript date Explanation > 12 is
    13 is
    14
    var myDate = new new  a Date (); 
    myDate.getYear ();
    myDate.getFullYear ();
    myDate.getMonth ();
    myDate.getDate (); // get the current date (1-31)
    myDate.getDay (); // get the current week X (0-6,0 for Sunday)
    myDate.getTime (); // get the current time (the number of milliseconds from the start 1970.1.1)
    myDate.getHours (); // get the current number (0-23 hours )
    myDate.getMinutes (); // get the current number of minutes (0-59)
    myDate.getSeconds (); // get the current number of seconds (0-59)
    myDate.getMilliseconds (); // get the current number of milliseconds (0- 999)
    myDate.toLocaleDateString (); // get the current date
    var MyTime = myDate.toLocaleTimeString (); // get the current time
    myDate.toLocaleString (); // get the date and time
  2. Timestamp get
    attention, java, php, etc. generated timestamp in seconds, not milliseconds, so when you need a signature time stamp, you need to turn the timestamp seconds

    1
    2
    var time = new Date();
    var timestamp = parseInt(time.getTime()/1000);
  3. Formatting time

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    // get the current time in the format DD-MM-YYYY 
    function () {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
    month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
    }

Guess you like

Origin www.cnblogs.com/lijianming180/p/12014367.html