get Time value from date value in long - Android java.util

Android examples for java.util:Date Time

Description

get Time value from date value in long

Demo Code

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {

  public static String getTime(long time) {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(time);/*from w ww  .j a v  a 2 s  . c o  m*/
    int minute = c.get(Calendar.MINUTE);
    int second = c.get(Calendar.SECOND);
    String times = "";
    if (minute < 10) {
      times = "0" + minute;
    } else {
      times = "" + minute;
    }
    if (second < 10) {
      times += ":0" + second;
    } else {
      times += ":" + second;
    }
    return times;
  }

  public static int[] getTime(long time, String timeZone) {
    Calendar c = Calendar.getInstance();
    if (timeZone != null && timeZone.length() != 0) {
      TimeZone tz = TimeZone.getTimeZone(timeZone);
      c = Calendar.getInstance(tz);
    }
    c.setTimeInMillis(time);
    int y = c.get(Calendar.YEAR);
    int m = c.get(Calendar.MONTH);
    int d = c.get(Calendar.DAY_OF_MONTH);
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);
    int second = c.get(Calendar.SECOND);
    return new int[] { y, m, d, hour, minute, second };
  }

  public static String getTime(String user_time) {
    String re_time = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date d;
    try {

      d = sdf.parse(user_time);
      long l = d.getTime();

      String str = String.valueOf(l);
      // re_time = str.substring(0, 10);
      re_time = str;

    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return re_time;
  }

  public static String getTime() {
    String re_time = null;
    long currentTime = System.currentTimeMillis();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
    Date d;
    d = new Date(currentTime);
    long l = d.getTime();
    String str = String.valueOf(l);
    re_time = str.substring(0, 10);
    return re_time;
  }

}

Related Tutorials