6 回复
楼主你好,HarmonyOS获取当前时间可以参考以下代码:
JS:
var currentDate= new Date();
currentDate.toLocaleTimeString();
Java:
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.applyPattern("yyyy-MM-dd HH:mm:ss ");
Date date = new Date();
sdf.format(date));
更多关于HarmonyOS 鸿蒙Next中获取当前时间的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
楼主是用Java开发,还是JS开发?不管理Java或JS都是和之前一样的获取方法。
js:new Date()[.getMonth(), .getDate()…]
java: LocalDate.now()[.getMonthValue(), getDate()…]
这个随便百度一下都能知道
欢迎开发小伙伴们进来帮帮楼主
在HarmonyOS(鸿蒙Next)中,获取当前时间可以通过Date
类或System.currentTimeMillis()
方法实现。以下是示例代码:
import java.util.Date;
public class Main {
public static void main(String[] args) {
// 使用Date类获取当前时间
Date currentDate = new Date();
System.out.println("当前时间: " + currentDate);
// 使用System.currentTimeMillis()获取当前时间戳
long currentTimeMillis = System.currentTimeMillis();
System.out.println("当前时间戳: " + currentTimeMillis);
}
}
Date
类提供格式化时间,System.currentTimeMillis()
返回自1970年1月1日以来的毫秒数。