根据时间戳获取相应的日期和时间

System.currentTimeMillis()返回的是格林威治时间从1970年1月1日00时00分00秒开始到现在的总毫秒数,(注意是格林威治时间)北京时间是1970年01月01日08时00分00秒;

在我们平时开发时,如果服务器是windows搭建的往往会出现android上的时间和服务器上的时间相差8个小时。

System.currentTimeMillis()也是我们常说的Unix时间戳;

以下是通过时间戳获取日期和时间的一些方法:

public class MyActivity extends Activity {

    public static final String TAG = "MyActivity";

    private Context mContext;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mContext = this;
        Log.d(TAG, "当前时间 时间戳: " + System.currentTimeMillis());
        Log.d(TAG, "当前时间 完整时间 : " + timeStampToDate(System.currentTimeMillis()));
        Log.d(TAG, "当前时间 year: " + getYearByTimeStamp(System.currentTimeMillis()));
        Log.d(TAG, "当前时间 month: " + getMonthByTimeStamp(System.currentTimeMillis()));
        Log.d(TAG, "当前时间 day: " + getDayByTimeStamp(System.currentTimeMillis()));
        Log.d(TAG, "当前时间 Hour: " + getHourByTimeStamp(System.currentTimeMillis()));
        Log.d(TAG, "当前时间 minute: " + getMinuteByTimeStamp(System.currentTimeMillis()));
        Log.d(TAG, "当前时间 Second: " + getSecondByTimeStamp(System.currentTimeMillis()));
        Log.d(TAG, "两个时间戳的日期是否相等: " + isTwoTimeStampDayEqual(System.currentTimeMillis(), System.currentTimeMillis()));

    }

    public static String timeStampToDate(long timeStamp){
        Date             date = new Date(timeStamp);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = simpleDateFormat.format(date);
        return dateStr;
    }

    public static int getYearByTimeStamp(long timeStamp){
        String date = timeStampToDate(timeStamp);
        String year = date.substring(0, 4);
        return Integer.parseInt(year);
    }

    public static int getMonthByTimeStamp(long timeStamp){
        String date = timeStampToDate(timeStamp);
        String month = date.substring(5, 7);
        return Integer.parseInt(month);
    }

    public static int getDayByTimeStamp(long timeStamp){
        String date = timeStampToDate(timeStamp);
        String day = date.substring(8, 10);
        return Integer.parseInt(day);
    }

    public static int getHourByTimeStamp(long timeStamp){
        String date = timeStampToDate(timeStamp);
        String hour = date.substring(11, 13);
        return Integer.parseInt(hour);
    }

    public static int getMinuteByTimeStamp(long timeStamp){
        String date = timeStampToDate(timeStamp);
        String minute = date.substring(14, 16);
        return Integer.parseInt(minute);
    }

    public static int getSecondByTimeStamp(long timeStamp){
        String date = timeStampToDate(timeStamp);
        String second = date.substring(17, 19);
        return Integer.parseInt(second);
    }

    //判断两个时间戳是否为同一天
    public static boolean isTwoTimeStampDayEqual(long firstTimeStamp, long secondTimeStamp){
        if(getYearByTimeStamp(firstTimeStamp) == getYearByTimeStamp(secondTimeStamp) &&
                getMonthByTimeStamp(firstTimeStamp) == getMonthByTimeStamp(secondTimeStamp)
                && getDayByTimeStamp(firstTimeStamp) == getDayByTimeStamp(secondTimeStamp)){
            return true;
        }
        return false;
    }
    //将时间字符串转换为Date
    public static Date stringToDate(String dateString){
        ParsePosition position = new ParsePosition(0);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date dateValue = simpleDateFormat.parse(dateString, position);
        return dateValue;
    }
}


  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值