Android - How to add days to date - android

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());

Related

How to fetch recent seven days from specific date

If the date is 2017-03-30 that i want to fetch the date from 2017-03-23 to 2017-03-30
I try to use this code let my String change to Date format
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dateParse = sdf.parse("2017-03-30");
then i'm stuck , cause i take the reference is get the current time like this
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -7);
//may be my dateParse should put here , but i don't know how to do
Date monday = c.getTime();//it get the current time
String preMonday = sdf.format(monday);
Is any one can teach me how to fetch these seven days ? Thanks in advance.
You can use the code below
SimpleDateFormatdateFormat = new SimpleDateFormat("dd MMM yyyy");
Calendar c = Calendar.getInstance();
String date = dateFormat.format(c.getTime());
c.add(Calendar.DATE, 7);
String date1 = dateFormat.format(c.getTime());
Parse the date:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = sdf.parse("2017-03-30");
First Solution 1) And then either figure out how many milliseconds you need to subtract:
Date newDate = new Date(myDate.getTime() - 604800000L); // 7 * 24 * 60 * 60 * 1000
Second Solution 2) Or use the API provided by the java.util.Calendar class:
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -7);
Date newDate = calendar.getTime();
Then, if you need to, convert it back to a String:
String date = dateFormat.format(newDate);
This answer is from here
EDIT:
If you need output as 2017-03-29 2017-03-28 2017-03-27 ...... 2017-03-23 then try below code
for(int i = 1; i <= 7; i++){
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -i);
Date newDate = calendar.getTime();
String date = dateFormat.format(newDate);
//here in date you can get all date from and output as 2017-03-29 2017-03-28 2017-03-27 ...... 2017-03-23
}
Hope you need this

Android Calendar DateFormat regional behavior

In my android application I get the time and the date with the following code:
Calendar calendar = Calendar.getInstance();
String date = DateFormat.getDateInstance().format(calendar.getTime());
String time = DateFormat.getTimeInstance().format(calendar.getTime());
In my Galaxy Note 4, I got:
Date: 29.07.2016
Time: 14:10:30
In Huawei Mate 8:
Date: 29.07.2016
Time: 02:10:30 nachm (what means pm).
On some customer devices it has some more formats.
So, my question is how can I get date and time on all devices and regions in the same format?
I want dd.mm.yyyy for date and hh:mm:ss for time.
One options is SimpleDateFormat
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dataFormater = new SimpleDateFormat();
dataFormater.applyPattern("dd.MM.yyyy");
String date = dataFormater.format(calendar.getTime());
dataFormater.applyPattern("hh:mm:ss");
String time = dataFormater.format(calendar.getTime());
I think date will be ok for you so You have to manually format the time from calendar like below.
public static final String TIME_FORMAT = "hh:mm:ss";
SimpleDateFormat TimeFormat = new SimpleDateFormat(TIME_FORMAT);
Calendar ATime = Calendar.getInstance();
String Timein12hourFormat = TimeFormat.format(ATime.getTime());
May this help you.Let me know.

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()

displaying date in edit text 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 .

Calendar not returning GMT

I am trying to get a calendar object set to GMT, but the getTime() always returns the time in GMT+1 (my current time). I have tried:
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("skeniver"));
They all apparently return GMT, because
cal.getTimeZone().getDisplayName()
returns "GMT+00:00"; but
cal.getTime().toString();
always displays the time in GMT+1.
Does anyone have any idea why this is happening?
You need to adjust for daylight savings. I'm not sure if this will help but it's code I use for adjusting any timezone to UTC in an app that's currently being used by a number of people around the world. I use Date instead of Calendar but it works...
Date dateTimeNow = new Date();
TimeZone tz = TimeZone.getDefault();
int currentOffsetFromUTC = tz.getRawOffset() + (tz.inDaylightTime(dateTimeNow) ? tz.getDSTSavings() : 0);
Date dateTimeNowUTC = new Date(dateTimeNow.getTime() - currentOffsetFromUTC);
If you want to in string then prefer the DateFormat or SimpleDateFormat for this
here is example
SimpleDateFormat sdf = new SimpleDateFormat(); // here you can also define your format of date for e.g. "dd/MM/yyyy z"
sdf.setTimeZone("GMT");
Calendar cal = Calendar.getInstance();
System.out.println(sdf.format(cal.getTime()));
Calendar.getTime() returns a Date object. In Java, a Date is just a holder to a long timestamp starting in the UNIX epoch.
To display a Date in a different TimeZone than the default, you can use a SimpleDateFormat.

Categories

Resources