gpt4 book ai didi

android - 在android中获取格式化的日期和时间

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:34:31 27 4
gpt4 key购买 nike

我有两个计算日期和时间的函数。

  public static String getFormattedDate(String time) {
long timeUnix = Long.parseLong(time);
Date myDate = new Date(timeUnix * 1000);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM d yy");
return simpleDateFormat.format(myDate);
}

public static String getFormattedTime(String time) {
long timeUnix = Long.parseLong(time);
Date myDate = new Date(timeUnix * 1000);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss");
return simpleDateFormat.format(myDate);
}

我从他们那里得到了我需要的正确输出。

Jul 12 11(date function) and 12:09:45(time function)

我如何从中计算天数以及如何设置上午/下午。

我正在尝试使用 am/pm 设置时间,如果它是今天的日期,如果它早于显示日期(周一、周二、周三等),如果它超过一周,则显示“MMM d yy ".

最佳答案

对于上午/下午的表示法:

SimpleDateFormat dateFormat = new SimpleDateFormat("KK:mm:ss a");
return dateFormat.format(myDate);

KK 是上午/下午 (0-11) 中的小时,而 HH 是一天中的小时 (0-23)

关于android - 在android中获取格式化的日期和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6660443/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com