Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current date using widget TextClock in android?

Tags:

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...

http://i.stack.imgur.com/oQ5VR.png

like image 924
Priya Avatar asked Jul 07 '16 05:07

Priya


3 Answers

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" />
like image 105
KPS250 Avatar answered Nov 14 '22 22:11

KPS250


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

like image 22
madhan kumar Avatar answered Nov 14 '22 21:11

madhan kumar


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" />
like image 43
Mohammad Sohail Avatar answered Nov 14 '22 21:11

Mohammad Sohail