java Android studio - Textview 显示日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40310773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:06:39  来源:igfitidea点击:

Android studio - Textview show date

javaandroidtextviewandroid-date

提问by Ivan

I've read a lot of posts about this, but I cannot get it to work. I need a textView (id - datumprikaz ) to show the current date like this: 28.11.2016.

我已经阅读了很多关于此的帖子,但我无法让它发挥作用。我需要一个 textView (id - datumprikaz ) 来显示当前日期,如下所示:28.11.2016。

I've managed to add the date using this method in my java:

我已经设法在我的 java 中使用此方法添加日期:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView dateView = (TextView)findViewById(R.id.datumprikaz);
    setDate(dateView);

}

public void setDate (TextView view){
    String str = String.format("%tc", new Date());
    view.setText(str);
}

The problem is I get as a result: Fri Oct 28 19:57:37 GMT

问题是我得到的结果是:格林威治标准时间 10 月 28 日星期五 19:57:37

And I just need it to show the current date like 28.11.2016.

我只需要它显示当前日期,如 28.11.2016。

How do I do this ?

我该怎么做呢 ?

I've tried another method with

我试过另一种方法

    String trenutniDatum = DateFormat.getDateTimeInstance().format(new Date());
    datumprikaz textView = (datumprikaz) findViewById(R.id.datumprikaz);
    datumprikaz.setText(trenutniDatum);

But the setText is red and won't work.

但是 setText 是红色的并且不起作用。

How do I get this ? 28.11.2016 in a textView with the id datumprikaz

我怎么得到这个?28.11.2016 在带有 id datumprikaz 的 textView 中

回答by Real73

what you need is just customizing you date.Here is simple solution for your setDate method.

您需要的只是自定义您的日期。这是您的 setDate 方法的简单解决方案。

public void setDate (TextView view){

    Date today = Calendar.getInstance().getTime();//getting date
    SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");//formating according to my need
    String date = formatter.format(today);
    view.setText(date);
}

you can format a date in many, many different ways. Here are some of the most common custom date formats.

您可以通过许多不同的方式格式化日期。以下是一些最常见的自定义日期格式。

yyyy-MM-dd results in 2009-09-06

yyyyMMdd results in 20090906

EEE MMM dd hh:mm:ss yyyy results in Sun Sep 06 08:32:51 2009

yyyy-MM-dd 结果于 2009-09-06

yyyyMMdd 结果为 20090906

EEE MMM dd hh:mm:ss yyyy 结果于 Sun Sep 06 08:32:51 2009

Hope this will help.

希望这会有所帮助。

回答by okarakose

you can use SimpleDateFormat

你可以使用SimpleDateFormat

TextView textView = (TextView)findViewById(R.id.datumprikaz);
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy", Locale.getDefault());
textView.setText(dateFormatter.format(trenutniDatum));

回答by Sergey D

Try this

试试这个

public void setDate (TextView view) {
String str = new SimpleDateFormat("dd.MM.yyyy", Locale.getDefault()).format(new Date());
view.setText(str); }

回答by Casey Perkins

The built-in TextClock is very convenient for displaying formatted date/time information, without any need to attend to constantly updating the content of the TextView.

内置的 TextClock 非常方便地显示格式化的日期/时间信息,而无需关注不断更新 TextView 的内容。

<android.widget.TextClock
    android:id="@+id/datumprikaz"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:format12Hour="dd.MM.yyyy"
    android:format24Hour="@null"
    />