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
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" />
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
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With