it编程 > App开发 > Android

Android实现获取当前时间并转为时间戳

12人参与 2025-09-30 Android

在项目开发中,难免会遇到使用当前时间,比如实现网络请求上传报文、预约、日历等功能。

1. 获取年月日时分秒

在获取时间之前,首先要引入simpledateformat:

import java.text.simpledateformat;

实现代码:

simpledateformat formatter = new simpledateformat("yyyy年mm月dd日 hh:mm:ss");
date curdate = new date(system.currenttimemillis());//获取当前时间       
string str  = formatter.format(curdate);

str就是我们需要的时间,代码中(“yyyy年mm月dd日 hh:mm:ss”)这个时间的样式是可以根据我们的需求进行修改的,比如:20170901112253 ==> (“yyyymmddhhmmss”)

如果只想获取年月,代码如下:

simpledateformat formatter = new simpledateformat("yyyy-mm");
date curdate = new date(system.currenttimemillis());//获取当前时间       
string str  = formatter.format(curdate);

2. 区分系统时间是24小时制还是12小时制

在获取之前,首先要引入contentresolver:

import android.content.contentresolver;

代码如下:

contentresolver cv = this.getcontentresolver();
string strtimeformat = android.provider.settings.system.getstring(cv,
                android.provider.settings.system.time_12_24);

if(strtimeformat.equals("24"))
{
   log.i("activity","24");
}

3. 字符串转时间戳

代码如下:

    //字符串转时间戳
    public static string gettime(string timestring){
        string timestamp = null;
        simpledateformat sdf = new simpledateformat("yyyy年mm月dd日 hh:mm");
        date d;
        try{
            d = sdf.parse(timestring);
            long l = d.gettime();
            timestamp = string.valueof(l);
        } catch(parseexception e){
            e.printstacktrace();
        }
        return timestamp;
    }

4. 时间戳转字符串

代码如下:

    //时间戳转字符串
    public static string getstrtime(string timestamp){
        string timestring = null;
        simpledateformat sdf = new simpledateformat("yyyy年mm月dd日 hh:mm");
        long  l = long.valueof(timestamp);
        timestring = sdf.format(new date(l));//单位秒
        return timestring;
    }

方法补充

1.android中获取当前时间戳

android获取时间戳的四种方法如下:

long timecurrenttimemillis = system.currenttimemillis();
long timegettime =new date().gettime();
long timeseconds = system.currenttimemillis();
long timemillis = calendar.getinstance().gettimeinmillis();

log.d(tag, "  当前时间戳1--->:"+timecurrenttimemillis);
log.d(tag, "  当前时间戳2--->:"+timegettime);
log.d(tag, "  当前时间戳3--->:"+timeseconds);
log.d(tag, "  当前时间戳4--->:"+timemillis);

2.android实现 时间戳与日期间的各种互换的时间工具类

完整代码如下:

package com.pts.peoplehui.utils;  
  
import java.text.simpledateformat;  
import java.util.calendar;  
import java.util.date;  
import java.util.locale;  
  
public class dateutils {  
  
    public static string gettodaydatetime() {  
        simpledateformat format = new simpledateformat(“yyyy-mm-dd hh:mm:ss”,  
                locale.getdefault());  
        return format.format(new date());  
    }  
  
    /** 
     * 掉此方法输入所要转换的时间输入例如(”2014年06月14日16时09分00秒”)返回时间戳 
     *  
     * @param time 
     * @return 
     */  
    public string data(string time) {  
        simpledateformat sdr = new simpledateformat(“yyyy年mm月dd日hh时mm分ss秒”,  
                locale.china);  
        date date;  
        string times = null;  
        try {  
            date = sdr.parse(time);  
            long l = date.gettime();  
            string stf = string.valueof(l);  
            times = stf.substring(0, 10);  
        } catch (exception e) {  
            e.printstacktrace();  
        }  
        return times;  
    }  
  
    public static string gettodaydatetimes() {  
        simpledateformat format = new simpledateformat(“mm月dd日”,  
                locale.getdefault());  
        return format.format(new date());  
    }  
      
    /** 
     * 获取当前时间 
     *  
     * @return 
     */  
    public static string getcurrenttime_today() {  
        simpledateformat sdf = new simpledateformat(“yyyy-mm-dd-hh-mm-ss”);  
        return sdf.format(new java.util.date());  
    }  
  
    /** 
     * 调此方法输入所要转换的时间输入例如(”2014-06-14-16-09-00”)返回时间戳 
     *  
     * @param time 
     * @return 
     */  
    public static string dataone(string time) {  
        simpledateformat sdr = new simpledateformat(“yyyy-mm-dd-hh-mm-ss”,  
                locale.china);  
        date date;  
        string times = null;  
        try {  
            date = sdr.parse(time);  
            long l = date.gettime();  
            string stf = string.valueof(l);  
            times = stf.substring(0, 10);  
        } catch (exception e) {  
            e.printstacktrace();  
        }  
        return times;  
    }  
  
    public static string gettimestamp(string time, string type) {  
        simpledateformat sdr = new simpledateformat(type, locale.china);  
        date date;  
        string times = null;  
        try {  
            date = sdr.parse(time);  
            long l = date.gettime();  
            string stf = string.valueof(l);  
            times = stf.substring(0, 10);  
        } catch (exception e) {  
            e.printstacktrace();  
        }  
        return times;  
    }  
  
    /** 
     * 调用此方法输入所要转换的时间戳输入例如(1402733340)输出(”2014年06月14日16时09分00秒”) 
     *  
     * @param time 
     * @return 
     */  
    public static string times(string time) {  
        simpledateformat sdr = new simpledateformat(“yyyy年mm月dd日hh时mm分ss秒”);  
        @suppresswarnings(“unused”)  
        long lcc = long.valueof(time);  
        int i = integer.parseint(time);  
        string times = sdr.format(new date(i * 1000l));  
        return times;  
  
    }  
      
    /** 
     * 调用此方法输入所要转换的时间戳输入例如(1402733340)输出(”2014-06-14  16:09:00”) 
     *  
     * @param time 
     * @return 
     */  
    public static string timedate(string time) {  
        simpledateformat sdr = new simpledateformat(“yyyy-mm-dd hh:mm:ss”);  
        @suppresswarnings(“unused”)  
        long lcc = long.valueof(time);  
        int i = integer.parseint(time);  
        string times = sdr.format(new date(i * 1000l));  
        return times;  
  
    }  
  
    /** 
     * 调用此方法输入所要转换的时间戳输入例如(1402733340)输出(”2014年06月14日16:09”) 
     *  
     * @param time 
     * @return 
     */  
    public static string timet(string time) {  
        simpledateformat sdr = new simpledateformat(“yyyy年mm月dd日  hh:mm”);  
        @suppresswarnings(“unused”)  
        long lcc = long.valueof(time);  
        int i = integer.parseint(time);  
        string times = sdr.format(new date(i * 1000l));  
        return times;  
  
    }  
  
    /** 
     * @param time斜杠分开 
     * @return 
     */  
    public static string timeslash(string time) {  
        simpledateformat sdr = new simpledateformat(“yyyy/mm/dd,hh:mm”);  
        @suppresswarnings(“unused”)  
        long lcc = long.valueof(time);  
        int i = integer.parseint(time);  
        string times = sdr.format(new date(i * 1000l));  
        return times;  
  
    }  
  
    /** 
     * @param time斜杠分开 
     * @return 
     */  
    public static string timeslashdata(string time) {  
        simpledateformat sdr = new simpledateformat(“yyyy/mm/dd”);  
        @suppresswarnings(“unused”)  
        long lcc = long.valueof(time);  
//      int i = integer.parseint(time);  
        string times = sdr.format(new date(lcc * 1000l));  
        return times;  
  
    }  
      
    /** 
     * @param time斜杠分开 
     * @return 
     */  
    public static string timeminute(string time) {  
        simpledateformat sdr = new simpledateformat(“hh:mm”);  
        @suppresswarnings(“unused”)  
        long lcc = long.valueof(time);  
        int i = integer.parseint(time);  
        string times = sdr.format(new date(i * 1000l));  
        return times;  
  
    }  
  
    public static string tim(string time) {  
        simpledateformat sdr = new simpledateformat(“yyyymmdd hh:mm”);  
        @suppresswarnings(“unused”)  
        long lcc = long.valueof(time);  
        int i = integer.parseint(time);  
        string times = sdr.format(new date(i * 1000l));  
        return times;  
    }  
  
    public static string time(string time) {  
        simpledateformat sdr = new simpledateformat(“yyyy-mm-dd hh:mm”);  
        @suppresswarnings(“unused”)  
        long lcc = long.valueof(time);  
        int i = integer.parseint(time);  
        string times = sdr.format(new date(i * 1000l));  
        return times;  
    }  
  
    // 调用此方法输入所要转换的时间戳例如(1402733340)输出(”2014年06月14日16时09分00秒”)  
    public static string times(long timestamp) {  
        simpledateformat sdr = new simpledateformat(“mm月dd日  #  hh:mm”);  
        return sdr.format(new date(timestamp)).replaceall(“#”,  
                getweek(timestamp));  
  
    }  
  
    private static string getweek(long timestamp) {  
        int mydate = 0;  
        string week = null;  
        calendar cd = calendar.getinstance();  
        cd.settime(new date(timestamp));  
        mydate = cd.get(calendar.day_of_week);  
        // 获取指定日期转换成星期几  
        if (mydate == 1) {  
            week = ”周日”;  
        } else if (mydate == 2) {  
            week = ”周一”;  
        } else if (mydate == 3) {  
            week = ”周二”;  
        } else if (mydate == 4) {  
            week = ”周三”;  
        } else if (mydate == 5) {  
            week = ”周四”;  
        } else if (mydate == 6) {  
            week = ”周五”;  
        } else if (mydate == 7) {  
            week = ”周六”;  
        }  
        return week;  
    }  
  
    // 并用分割符把时间分成时间数组  
    /** 
     * 调用此方法输入所要转换的时间戳输入例如(1402733340)输出(”2014-06-14-16-09-00”) 
     *  
     * @param time 
     * @return 
     */  
    public string timesone(string time) {  
        simpledateformat sdr = new simpledateformat(“yyyy-mm-dd-hh-mm-ss”);  
        @suppresswarnings(“unused”)  
        long lcc = long.valueof(time);  
        int i = integer.parseint(time);  
        string times = sdr.format(new date(i * 1000l));  
        return times;  
  
    }  
  
    public static string timestwo(string time) {  
        simpledateformat sdr = new simpledateformat(“yyyy-mm-dd”);  
        @suppresswarnings(“unused”)  
        long lcc = long.valueof(time);  
        int i = integer.parseint(time);  
        string times = sdr.format(new date(i * 1000l));  
        return times;  
  
    }  
  
    /** 
     * 并用分割符把时间分成时间数组 
     *  
     * @param time 
     * @return 
     */  
    public static string[] timestamp(string time) {  
        simpledateformat sdr = new simpledateformat(“yyyy年mm月dd日hh时mm分ss秒”);  
        @suppresswarnings(“unused”)  
        long lcc = long.valueof(time);  
        int i = integer.parseint(time);  
        string times = sdr.format(new date(i * 1000l));  
        string[] fenge = times.split(”[年月日时分秒]”);  
        return fenge;  
    }  
  
    /** 
     * 根据传递的类型格式化时间 
     *  
     * @param str 
     * @param type 
     *            例如:yy-mm-dd 
     * @return 
     */  
    public static string getdatetimebymillisecond(string str, string type) {  
  
        date date = new date(long.valueof(str));  
  
        simpledateformat format = new simpledateformat(type);  
  
        string time = format.format(date);  
  
        return time;  
    }  
  
    /** 
     * 分割符把时间分成时间数组 
     *  
     * @param time 
     * @return 
     */  
    public string[] division(string time) {  
  
        string[] fenge = time.split(”[年月日时分秒]”);  
  
        return fenge;  
  
    }  
  
    /** 
     * 输入时间戳变星期 
     *  
     * @param time 
     * @return 
     */  
    public static string changeweek(string time) {  
        simpledateformat sdr = new simpledateformat(“yyyy年mm月dd日hh时mm分ss秒”);  
        long lcc = long.valueof(time);  
        int i = integer.parseint(time);  
        string times = sdr.format(new date(i * 1000l));  
        date date = null;  
        int mydate = 0;  
        string week = null;  
        try {  
            date = sdr.parse(times);  
            calendar cd = calendar.getinstance();  
            cd.settime(date);  
            mydate = cd.get(calendar.day_of_week);  
            // 获取指定日期转换成星期几  
        } catch (exception e) {  
            // todo auto-generated catch block  
            e.printstacktrace();  
        }  
        if (mydate == 1) {  
            week = ”星期日”;  
        } else if (mydate == 2) {  
            week = ”星期一”;  
        } else if (mydate == 3) {  
            week = ”星期二”;  
        } else if (mydate == 4) {  
            week = ”星期三”;  
        } else if (mydate == 5) {  
            week = ”星期四”;  
        } else if (mydate == 6) {  
            week = ”星期五”;  
        } else if (mydate == 7) {  
            week = ”星期六”;  
        }  
        return week;  
  
    }  
  
    /** 
     * 获取日期和星期 例如:2014-11-13 11:00 星期一 
     *  
     * @param time 
     * @param type 
     * @return 
     */  
    public static string getdateandweek(string time, string type) {  
        return getdatetimebymillisecond(time + “000”, type) + “  ”  
                + changeweekone(time);  
    }  
  
    /** 
     * 输入时间戳变星期 
     *  
     * @param time 
     * @return 
     */  
    public static string changeweekone(string time) {  
        simpledateformat sdr = new simpledateformat(“yyyy-mm-dd-hh-mm-ss”);  
        long lcc = long.valueof(time);  
        int i = integer.parseint(time);  
        string times = sdr.format(new date(i * 1000l));  
        date date = null;  
        int mydate = 0;  
        string week = null;  
        try {  
            date = sdr.parse(times);  
            calendar cd = calendar.getinstance();  
            cd.settime(date);  
            mydate = cd.get(calendar.day_of_week);  
            // 获取指定日期转换成星期几  
        } catch (exception e) {  
            // todo auto-generated catch block  
            e.printstacktrace();  
        }  
        if (mydate == 1) {  
            week = ”星期日”;  
        } else if (mydate == 2) {  
            week = ”星期一”;  
        } else if (mydate == 3) {  
            week = ”星期二”;  
        } else if (mydate == 4) {  
            week = ”星期三”;  
        } else if (mydate == 5) {  
            week = ”星期四”;  
        } else if (mydate == 6) {  
            week = ”星期五”;  
        } else if (mydate == 7) {  
            week = ”星期六”;  
        }  
        return week;  
  
    }  
  
    /** 
     * 获取当前时间 
     *  
     * @return 
     */  
    public static string getcurrenttime() {  
        simpledateformat sdf = new simpledateformat(“yyyy-mm-dd hh:mm”);  
        return sdf.format(new java.util.date());  
    }  
      
    /** 
     * 输入日期如(2014年06月14日16时09分00秒)返回(星期数) 
     *  
     * @param time 
     * @return 
     */  
    public string week(string time) {  
        date date = null;  
        simpledateformat sdr = new simpledateformat(“yyyy年mm月dd日hh时mm分ss秒”);  
        int mydate = 0;  
        string week = null;  
        try {  
            date = sdr.parse(time);  
            calendar cd = calendar.getinstance();  
            cd.settime(date);  
            mydate = cd.get(calendar.day_of_week);  
            // 获取指定日期转换成星期几  
        } catch (exception e) {  
            // todo auto-generated catch block  
            e.printstacktrace();  
        }  
        if (mydate == 1) {  
            week = ”星期日”;  
        } else if (mydate == 2) {  
            week = ”星期一”;  
        } else if (mydate == 3) {  
            week = ”星期二”;  
        } else if (mydate == 4) {  
            week = ”星期三”;  
        } else if (mydate == 5) {  
            week = ”星期四”;  
        } else if (mydate == 6) {  
            week = ”星期五”;  
        } else if (mydate == 7) {  
            week = ”星期六”;  
        }  
        return week;  
    }  
  
    /** 
     * 输入日期如(2014-06-14-16-09-00)返回(星期数) 
     *  
     * @param time 
     * @return 
     */  
    public string weekone(string time) {  
        date date = null;  
        simpledateformat sdr = new simpledateformat(“yyyy-mm-dd-hh-mm-ss”);  
        int mydate = 0;  
        string week = null;  
        try {  
            date = sdr.parse(time);  
            calendar cd = calendar.getinstance();  
            cd.settime(date);  
            mydate = cd.get(calendar.day_of_week);  
            // 获取指定日期转换成星期几  
        } catch (exception e) {  
            // todo auto-generated catch block  
            e.printstacktrace();  
        }  
        if (mydate == 1) {  
            week = ”星期日”;  
        } else if (mydate == 2) {  
            week = ”星期一”;  
        } else if (mydate == 3) {  
            week = ”星期二”;  
        } else if (mydate == 4) {  
            week = ”星期三”;  
        } else if (mydate == 5) {  
            week = ”星期四”;  
        } else if (mydate == 6) {  
            week = ”星期五”;  
        } else if (mydate == 7) {  
            week = ”星期六”;  
        }  
        return week;  
    }  
}  

3.android获取时间戳,以及将时间戳转换为时间

代码如下:

package com.androidtimestampdemo;  
  
import java.text.simpledateformat;  
import java.util.calendar;  
import java.util.date;  
import java.util.locale;  
  
import android.app.activity;  
import android.os.bundle;  
import android.util.log;  
  
public class mainactivity extends activity {  
    private long timecurrenttimemillis;  
    private long timegettime;  
    private long timeseconds;  
    private long timemillis;  
  
    @override  
    protected void oncreate(bundle savedinstancestate) {  
        super.oncreate(savedinstancestate);  
        setcontentview(r.layout.activity_main);  
  
        init();  
    }  
  
    private void init() {  
        /** 
         * 以下是获取现在的系统时间戳的几种方法,实际开发中可能需要服务端返回所需的时间戳 ; 
         * 但是实际开发中服务端返回的时间戳可能是字符串等,需要转换为long等,可采用如下方法直接转换: 
         * integer.parseint(string s) long.parselong(string s) 
         * float.parsefloat(string s) double.parsedouble(string s) 
         */  
        timecurrenttimemillis = system.currenttimemillis();  
        timegettime = new date().gettime();  
        timeseconds = system.currenttimemillis();  
        timemillis = calendar.getinstance().gettimeinmillis();  
        /** 
         * 通过打印信息可以看到,这几种获取系统时间的时间戳几乎是一样的。 
         */  
        log.d("zhongyao", "timecurrenttimemillis" + timecurrenttimemillis  
                + "@@@" + "timegettime" + timegettime + "@@@timeseconds"  
                + timeseconds + "timemillis" + timemillis);  
        /** 
         * 时间戳转换成具体时间形式 
         */  
        simpledateformat sdf = new simpledateformat("yyyy年mm月dd日",  
                locale.getdefault());  
        string time1 = sdf.format(timeseconds);  
        string time2 = sdf.format(timegettime);  
        log.d("zhongyao", timeseconds + "  现在的时间1->:" + time1);  
        log.d("zhongyao", timegettime + "  现在的时间2-->:" + time2);  
  
        simpledateformat sdftwo = new simpledateformat("yyyy年mm月dd日hh时mm分ss秒e",  
                locale.getdefault());  
        string time11 = sdftwo.format(timeseconds);  
        string time22 = sdftwo.format(timegettime);  
        log.d("zhongyao", timeseconds + "  现在的时间11->:" + time11);  
        log.d("zhongyao", timegettime + "  现在的时间22-->:" + time22);  
          
        /** 
         * 而最常用的: 
         * 由于服务端返回的一般是unix时间戳,所以需要把unix时间戳timestamp转换成固定格式的字符串 
         */  
        string result = formatdata("yyyy-mm-dd", 1414994617);  
        log.d("zhongyao", result);  
  
    }  
  
    public static string formatdata(string dataformat, long timestamp) {  
        if (timestamp == 0) {  
            return "";  
        }  
        timestamp = timestamp * 1000;  
        string result = "";  
        simpledateformat format = new simpledateformat(dataformat);  
        result = format.format(new date(timestamp));  
        return result;  
    }  
}  

到此这篇关于android实现获取当前时间并转为时间戳的文章就介绍到这了,更多相关android获取当前时间内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

您想发表意见!!点此发布评论

推荐阅读

Android 自定义Binding Adapter实战应用及作用详解

09-28

Android开发教程之屏幕变更事件

09-28

Android 中的 mk 和 bp 文件编译示例详解

10-10

Android自定义电池组件实例代码(BatteryView)

09-23

Android杀死选中进程并释放进程占用空间的方法详解

10-12

Android通过Dialog实现全屏的示例代码

10-12

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论