简体   繁体   中英

Android studio - Textview show date

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.

I've managed to add the date using this method in my 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

And I just need it to show the current date like 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.

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

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

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

Hope this will help.

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.

<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"
    />

you can use SimpleDateFormat

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

Try this

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

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date → text), parsing (text → date), and normalization.

Example:

import java.text.SimpleDateFormat;

import java.util.Calendar;

///

Calendar calendar;

calendar = Calendar.getInstance();

/////

SimpleDateFormat simpleDateFormat;

simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss aaa z");

// For your case : simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");

////

String dateTime;

dateTime = simpleDateFormat.format(calendar.getTime()).toString();
    
   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM