Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set date and desired time in Android

Tags:

java

android

I want to set date and time in my android app. The date should be today's date but the time should be set to 6:00 AM by default in the text field. I have read many links but most of them shows today's time and date (example: 2016-03-28 11:53:55).

 String timetxt1 = "06:00:00";
 Date datetxt1 = null;;
 try {
     datetxt1 = simpleDateFormat.parse(timetxt1);
 } catch (ParseException e) {
     e.printStackTrace();
 }
 Calendar selectedDate1 = Calendar.getInstance();
 selectedDate1.setTime(datetxt1);

 edittxt.setText(dateFormatter.format(selectedDate1.getTime()));
like image 570
Mew Avatar asked Mar 28 '16 06:03

Mew


People also ask

Why is my automatic date and time wrong Android?

Turn on Android's automatic date/time setting. Do this through Settings > System > Date & time. Select the button next to Set time automatically to trigger it. If this is already turned on, turn it off, restart your phone, and then turn it back on.


Video Answer


1 Answers

 Date date = new Date();
 Calendar calendar = Calendar.getInstance();
 calendar.setTime(date);
 calendar.set(Calendar.HOUR_OF_DAY, 6);// for 6 hour
 calendar.set(Calendar.MINUTE, 0);// for 0 min
 calendar.set(Calendar.SECOND, 0);// for 0 sec
 System.out.println(calendar.getTime());// print 'Mon Mar 28 06:00:00 ALMT 2016'
like image 82
mmuzahid Avatar answered Sep 28 '22 13:09

mmuzahid