SoFunction
Updated on 2025-04-09

Android get time instance code

Android get time instance code

Notice:

h: 12-hour hours
H: 24-hour hours

Example code:

import ;
import ;
import ;
import ;
import ;

/**
 * Created by Administrator on 2017/5/8.
 */
public class GetTime {

  public static void main(String[] args) {
    Date date = new Date();
    (date);//Mon May 08 14:27:44 CST 2017
    (new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date));//2017-05-08 02:27:44

    long millis = ();
    (millis);//1494224864479
    (new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(millis));//2017-05-08 02:27:44

    //yyyy-MM-dd  E  hh:mm:
    //Year-Month-Day Week Hours: Minutes: Seconds. Milliseconds    (new SimpleDateFormat("yyyy-MM-dd E hh:mm:").format(date));//2017-05-08 Monday 02:27:44.044    (new SimpleDateFormat("yyyy-MM-dd HH:mm:").format(date));//2017-05-08 14:27:44.044
    (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));//2017-05-08 14:27:44
    (new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date));//2017-05-08 14:27
    (new SimpleDateFormat().format(date));//17-5-8 2:27 pm: Default
    compareDataToNow("2017-05-03 12:45:00");

    try {
      Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2017-05-03 12:45:00");
      compareToNowDate(date1);
    } catch (ParseException e) {
      ();
    }

    getWeek();
    getTime1();
    getTime2();
  }


  static void getTime1() {
    long time = ();
    //long now = ();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date d1 = new Date(time);
    String t1 = (d1);
    ("SimpleDateFormattime===" + t1);//2017-05-08 12:44:10

    SimpleDateFormat f4 = new SimpleDateFormat("Today is" + "Yyyyy MM month dd day E kk point mm point");
    ("f4======" + (new Date()));//Today is Monday, May 8, 2017 at 14:15
    SimpleDateFormat f3 = new SimpleDateFormat("Today is" + "hhhhmm minutes");
    ("f3======" + (new Date()));//Today is 02 hours and 15 minutes
    SimpleDateFormat f2 = new SimpleDateFormat("Today is" + "Kk point mm minutes");
    ("f2======" + (new Date()));//Today is 14:17 minutes  }

  static void getTime2() {
    Calendar calendar = ();
    String created = () + "Year"
        + (() + 1) + "moon"// Calculate from 0        + (Calendar.DAY_OF_MONTH) + "day"
        + (Calendar.HOUR_OF_DAY) + "hour"
        + () + "point" + () + "s";
    ("Calendartime====" + created);//Time: 12:33 pm on May 8, 2017 24s  }

  static void getWeek() {
    Calendar calendar = ();
    int day = (Calendar.DAY_OF_WEEK);
    String today = null;
    if (day == 2) {
      today = "Monday";
    } else if (day == 3) {
      today = "Tuesday";
    } else if (day == 4) {
      today = "Wednesday";
    } else if (day == 5) {
      today = "Thursday";
    } else if (day == 6) {
      today = "Friday";
    } else if (day == 7) {
      today = "Saturday";
    } else if (day == 1) {
      today = "Sunday";
    }
    ("Today is:- " + today);//Today is:- Monday
  }

  //How many days apart between calculation dates:  static long compareDataToNow(String date) {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date passDate, nowDate;
    long diff = -100l, days = -100l;

    try {
      passDate = (date);

      String nowStr = (new Date());
      nowDate = (nowStr);

      diff = () - ();//The number of milliseconds of long type      days = diff / (1000 * 60 * 60 * 24);
      ("Distance:" + days + "sky" + " ()=====" + ());//-5 days    } catch (ParseException e) {
      ();
    }
    return diff;
  }

 //How many days apart between calculation dates:  static long compareToNowDate(Date date) {
    long diff = -100l, days = -100l;

    Date nowDate = new Date();

    diff = () - ();//The number of milliseconds of long type    days = diff / (1000 * 60 * 60 * 24);
    ("Distance:" + days + "sky" + " ()=====" + ());//-5 days
    return diff;
  }
}

Thank you for reading, I hope it can help you. Thank you for your support for this site!