Android Studio开发获取本地时间

Posted 秦汉思源

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Studio开发获取本地时间相关的知识,希望对你有一定的参考价值。

实现本地聊天记录功能,用到获取本地时间的函数,感觉比较实用,分享给大家!

1、在相应moudle的java文件夹下建立自己的util文件夹如·:

2、建立DateUtil类

package com.example.junior.util;//包名

import android.annotation.SuppressLint;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


public class DateUtil {
    @SuppressLint("SimpleDateFormat")
    public static String getNowDateTime() {
        SimpleDateFormat s_format = new SimpleDateFormat("yyyyMMddhhmmss");
        return s_format.format(new Date());
    }

    @SuppressLint("SimpleDateFormat")
    public static String getNowTime() {
        SimpleDateFormat s_format = new SimpleDateFormat("HH:mm:ss");
        return s_format.format(new Date());
    }

}

 

3、在你所要的工程中导入import com.example.junior.util.DateUtil;

在需要调用的地方直接调用 DateUtil.getNowTime() 即可


以上是关于Android Studio开发获取本地时间的主要内容,如果未能解决你的问题,请参考以下文章

我的Android进阶之旅Android Studio 中 使用git提交代码报错:Can‘t commit changes from multiple changelists at once(代码片

Android studio提示Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.(代码片

Android studio提示Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.(代码片

Android Studio Arctic Fox的Git Log和搜索窗口中文乱码问题解决方案(关键词:AS,2020.3.1,显示错误,Find in Path,版块,方块,修复,新版本)(代码片

Android Studio编译OsmAnd出现警告:GeoPointParserUtil.java使用或覆盖了已过时的 API。有关详细信息请使用-Xlint:deprecation重新编译(代码片

如何在 NDK 原生代码中获取断点并在 Android Studio 中调试原生代码?