Priya
Priya

Reputation: 692

How to get current date using widget TextClock in android?

I have create a TextClock widget to show the running time.

In Layout

<TextClock
    android:id="@+id/timerr"
    android:layout_width="wrap_content"
    android:layout_centerInParent="true"
    android:textSize="50sp"
    android:layout_height="wrap_content"
  />

In activity , created reference

    textClock = (TextClock) findViewById(R.id.timerr);

Setted format for both 12 hours and 24 hour format

For 24 hour

    textClock.setFormat12Hour(null);
    textClock.setFormat24Hour("HH:mm:ss");

for 12 hour,

   textClock.setFormat12Hour("hh:mm:ss a");
   textClock.setFormat24Hour(null);

Now,i need to get the current date while clicked the button along with the time,but i need to display only the time not the date in UI.

it is possible to get the Date along with time,someone help plz.. Thanks in advance.

This is how my UI looks...

https://i.sstatic.net/oQ5VR.png

Upvotes: 8

Views: 10521

Answers (3)

Mohammad Sohail
Mohammad Sohail

Reputation: 11

If you want to show date like example Wed, 27-Oct

use android:format12Hour="EE, dd-MMM"

as below for example

                 <TextClock
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:format12Hour="EE, dd-MMM"
                  android:textColor="@color/gray_100"
                  android:textSize="15sp"
                  android:layout_gravity="center"
                  android:padding="5dp"
                  android:textStyle="bold" />

Upvotes: 1

madhan kumar
madhan kumar

Reputation: 1608

You cannot get current date from time widget. But, using Date, Calendar with SimpleDateFormat is proper way to get currentdate.

1.

Date() + SimpleDateFormat()

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

Date date = new Date();

System.out.println(dateFormat.format(date)); 

2

Calender() + SimpleDateFormat()

 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");

 Calendar cal = Calendar.getInstance();

 System.out.println(dateFormat.format(cal.getTime()));

For more details refer this link

Upvotes: 1

KPS250
KPS250

Reputation: 315

Just use the format to retrieve date time

(dd:Mm:yyyy:hh:mm:ss a) --> 06-02-2018:08:13:57 PM

(dd:MMM:yyyy:hh:mm:ss a) --> 06-Feb-2018:08:13:57 PM

<TextClock
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:format12Hour="dd-MMM-yyyy:hh:mm:ss a"
    android:gravity="center_horizontal" />

Upvotes: 10

Related Questions