如何在Android中格式化日期和时间?

377

如何根据设备配置的日期和时间正确格式化具有年、月、日、小时和分钟的时间?

26个回答

2

尝试:

event.putExtra("startTime", "10/05/2012");

当您访问传递的变量时:

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(bundle.getString("startTime"));

1
回到2016年,当我想要自定义格式(而不是按照设备配置,如您所询问的...),我通常使用字符串资源文件:

在strings.xml中:

<string name="myDateFormat"><xliff:g id="myDateFormat">%1$td/%1$tm/%1$tY</xliff:g></string>

在活动中:
Log.d(TAG, "my custom date format: "+getString(R.string.myDateFormat, new Date()));

这也适用于新的日期绑定库的发布。
因此,我可以在布局文件中使用如下内容:
<TextView
    android:id="@+id/text_release_date"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:padding="2dp"
    android:text="@{@string/myDateFormat(vm.releaseDate)}"
    tools:text="0000"
    />

在 Java 类中:

    MovieDetailViewModel vm = new MovieDetailViewModel();
    vm.setReleaseDate(new Date());

1

避免使用 j.u.Date

Java.util.Date、.Calendar 和 SimpleDateFormat 在 Java(和 Android)中是出了名的麻烦。尽量避免使用它们。它们非常糟糕,以至于 Sun/Oracle 放弃了它们,并在 Java 8 中用新的 java.time 包替代它们(截至 2014 年,Android 中还没有)。新的 java.time 受到了 Joda-Time 库的启发。

Joda-Time

Joda-Time 在 Android 上可以使用。

搜索 StackOverflow 上的“Joda”以找到许多示例和讨论。

使用 Joda-Time 2.4 的一小段源代码。

标准格式。

String output = DateTime.now().toString(); 
// Current date-time in user's default time zone with a String representation formatted to the ISO 8601 standard.

本地化格式。

String output = DateTimeFormat.forStyle( "FF" ).print( DateTime.now() ); 
// Full (long) format localized for this user's language and culture.

0

虽然已经晚了,但可能对某些人有所帮助

DateFormat.format(format, timeInMillis);

这里的format是你需要的格式

例如:"HH:mm"返回15:30


0

android Time类提供了3种格式化方法 http://developer.android.com/reference/android/text/format/Time.html

这是我的实现方式:

/**
* This method will format the data from the android Time class (eg. myTime.setToNow())   into the format
* Date: dd.mm.yy Time: hh.mm.ss
*/
private String formatTime(String time)
{
    String fullTime= "";
    String[] sa = new String[2];

    if(time.length()>1)
    {
        Time t = new Time(Time.getCurrentTimezone());
        t.parse(time);
        // or t.setToNow();
        String formattedTime = t.format("%d.%m.%Y %H.%M.%S");
        int x = 0;

        for(String s : formattedTime.split("\\s",2))
        {   
            System.out.println("Value = " + s);
            sa[x] = s;
            x++;
        }
        fullTime = "Date: " + sa[0] + " Time: " + sa[1];
    }
    else{
        fullTime = "No time data";
    }
    return fullTime;
}

希望这对你有所帮助 :-)


-3

日期 - 类型

[Formatting and parsing date Times as strings][1]

EEE : Day ( Mon )
MMMM : Full month name ( December ) 
MMM : Month in words ( Dec )
MM : Month ( 12 )
dd : Day in 2 chars ( 03 )
d: Day in 1 char (3)
HH : Hours ( 12 )
mm : Minutes ( 50 )
ss : Seconds ( 34 )
yyyy: Year ( 2022 ) 
YYYY: Year ( 2022 )
zzz : GMT+05:30
a : ( AM / PM )
aa : ( AM / PM )
aaa : ( AM / PM )
aaaa : ( AM / PM )

1
你只需要复制并粘贴上述代码,尽量不要从其他地方复制答案。@AshishKumar - Tippu Fisal Sheriff

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接