3

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

Ivan
  • 816
  • 2
  • 9
  • 14

5 Answers5

8

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.

Real73
  • 490
  • 4
  • 13
  • TextView datumprikaz = (TextView)findViewById(R.id.datumprikaz); Date danas = new Date();//This creates a date representing this instance in time. SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); String novDatum = sdf.format(danas); datumprikaz.setText(novDatum); This worked, thank you ! – Ivan Oct 31 '16 at 14:10
4

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"
    />
Casey Perkins
  • 1,853
  • 2
  • 23
  • 41
0

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));
okarakose
  • 3,692
  • 5
  • 24
  • 44
0

Try this

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

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();