displaying date in edit text android - android

In my application i have edit text that will show the current date and time initially . If i change the date to some other date and when i tried to use gettext() i am still getting the current date. can anyone help. This is my code to set initial value in edit text
TaskTime = (EditText) findViewById(R.id.txtTaskTime);
Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
TaskDate.setText(dateFormat.format(date));

Most of the use of date object is obsolete in Android, you should use Calendar : http://developer.android.com/reference/java/util/Calendar.html

Yes, you can get the current date because , Date date = new Date(); this function always returns the current date .
So you need to use the Calendar object to get the current date and time .
Example
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String strDate = sdf.format(cal.getTime());
Here in dateFormat you have to declare your date format . dateFormat = "dd/MM/yyyy" like .

Related

Simpledateformat is not giving proper date

I want today's date in my application to store it in a database as a string but when i used simpledatefromat and calender.getInstance(), it's not giving me proper date. The code is as below.
I tried simpledateformat's another constructor which takes locale as parameter but still not showing proper date
Date date = Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
String dateF = dateFormat.format(date);
expected date should be 21-04-2019
actual result 21-26-2019
To get the month like the way that you want, you need to change "dd-mm-yyyy" to "dd-MM-yyyy"
Date date = Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
String dateF = dateFormat.format(date);

Android - How to add days to date

I have retrieved a Date from a SQLiteDatabase and have formatted it to how I want via the following;
String steepingDate = (c.getString(3));
SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy");
Date steepingdate = formatter.parse(steepingDate);
I now want to give the user the option to increase whatever date is in steepingdate by a certain amount of days that the user can input
I know you can use;
Date today = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, 10);
For example to add 10 days onto todays date
But how do you do it so that it uses steepingdate instead of todays date
Thanks
UPDATE;
The calendar is working as I want, but I now want to save the new data to the database, the full code is as following;
String steepingDate = (c.getString(3));
SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy");
Date steepingdate = formatter.parse(steepingDate);
Integer amountDays = Integer.parseInt(TSExtend.getText().toString());
Calendar ca = Calendar.getInstance();
ca.setTime(steepingdate);
ca.add(Calendar.DAY_OF_YEAR, amountDays);
DateFormat dateFormat = new SimpleDateFormat("dd/MMM/yyyy");
String newDate = dateFormat.format(ca);
I'm getting the error;
Bad class: class
java.util.GregorianCalendar
Any ideas?
To add 10 days to steepingdate, you can use:
Calendar calendar = Calendar.getInstance();
calendar.setTime(steepingdate);
calendar.add(Calendar.DAY_OF_YEAR, 10);
it the number is provided, through the user interface, you can use the View.OnClickListener and when onClick is fired, read the value from an EditText, and use this value instead of 10
Set the time of the calendar to your date, then add the days
Calendar cal = Calendar.getInstance();
cal.setTime(steepingdate);
cal.add(Calendar.DATE, 10);
UPDATE:
You can't directly format a Calendar, first get the Date from the Calendar, then format it.
String newDate = dateFormat.format(ca.getTime());

Getting the current time and date in the device's defined format

How can I get the current time and date according to phone device format?
my code is
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm aa",
Locale.ENGLISH);
String var = dateFormat.format(date));
It displays like 2PM instead of 14PM.
You can the device format data and time using
Date date = new Date();
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
Log.d(TAG,dateFormat.format(date)+");
Use calendar object instead of Date because it is deprecated now and by using caledar object you can get hour in 12 hours formate as
int hour = calendarObject.get(Calendar.HOUR);
for 24 formate as:
int hour = calendarObject.get(Calendar.HOUR_OF_DAY);
for further detail you can visit this
Just change "hh:mm aa" to "HH:mm aa"

How to get Current Date and time of Dubai in Android?

I am getting current date of the device, but i want the date and time of city Dubai, Any one please help me in doing this.when the activity called the date and time should be displayed,
Any help will be appriciated. thank you..
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parsed = format.parse("2011-03-01 15:10:37");
// => This time is in the user phone timezone, you will maybe need to turn it in UTC!
TimeZone tz = TimeZone.getTimeZone("Asia/Dubai");
format.setTimeZone(tz);
String result = format.format(parsed);
You can use TimeZone to pass to the SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Dubai"));

How to database.update the date value

I have a date value in this format: yyyy-MM-dd kk:mm:ss
So from this line data = params.getString("data"); i get the date I have set before in another activity.
So with a button click I need to add + 10 minutes to the date value.
I do know its through the value.put("...",...); but as I don't want to change the DATE only the TIME of the value. How should do I do it?
Use an instance of Calendar:
SimpleDateFormat currentDate = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
Calendar c = Calendar.getInstance();
c.setTime(currentDate.parse(data)); // your date value
c.add(Calendar.MINUTE,10);
newDate = c.getTime()

Categories

Resources