I want to add days like 30, 60, 90, etc to a date a timestamp like 1642599000000 and want to get the new timestamp. I am using the following code but not working properly. It only works for 30 days only.
Calendar c = Calendar.getInstance();
c.setTimeInMillis(initialTimestamp);
Date date= c.getTime();
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, 60); //Adding 60 days to current timestamp
long newTimestamp = cal.getTime().getTime(); //Required this
use c.add(Calendar.DATE, 60); instead DAY_OF_MONTH
DAY_OF_MONTH may be used for fetching number of day in current month (not year, not numer of all days fit into timestamp, which is huge and contains thousands of days)
Related
I have create a function for my code which set a rule to restrict user to select the day earlier then systems date from date picker dialog
protected void setMinMax() {
Calendar minAllowedDate = CommUtils.getMinAllowedDate();
minAllowedDate.add(Calendar.MILLISECOND, -1000);
datePickerDialog.getDatePicker().setMinDate(minAllowedDate.getTimeInMillis());
now i want to create another date picker for user to select DOB, what i want to do is to set a rules that user DOB should be on age 4-150 which means the YEAR that user can select should be -4 & -150 from systems date. If using the the code above, what should i put in
minAllowedDate.add(Calendar.MILLISECOND, -1000);
i had tried with below code which give me the year 150 as min option
minAllowedDate.add(Calendar.YEAR, -150);
Use the below for minimum and maximum
Calendar calStart = new GregorianCalendar();
calStart.setTime(new Date());
calStart.add(Calendar.YEAR, -150);
Calendar calEnd = new GregorianCalendar();
calEnd.setTime(new Date());
calEnd.add(Calendar.YEAR, -4);
You can use android:startYear which will include the start year.
Look at the documentation here.
Found out that most of the solution using the method .add(Calendar.YEAR,-4), however this also create limit to the month & date to be select. For example, When the current date is 11/Jun/2018, running that line of code you can have 4 years before(2018-4=2014) but also limit in your month + day which you can only select date before 11/Jun/2018.
After several tried, i had came out the solution which manually set the day & month to 31/Dec so that i can have an option range from 31/12/2014 to 1/1/2014
Calendar calStart = new GregorianCalendar();
calStart.setTime(new Date());
calStart.set(Calendar.MONTH, 11);
calStart.set(Calendar.DAY_OF_MONTH, 31);
calStart.add(Calendar.YEAR, -150);
Calendar calEnd = new GregorianCalendar();
calEnd.setTime(new Date());
calEnd.set(Calendar.MONTH, 11);
calEnd.set(Calendar.DAY_OF_MONTH, 31);
calEnd.add(Calendar.YEAR,-4);
datePickerDialog.getDatePicker().setMinDate(calStart.getTimeInMillis());
datePickerDialog.getDatePicker().setMaxDate(calEnd.getTimeInMillis());
so I have this code in Android
DateFormat formatter = new SimpleDateFormat("HH:mm", Locale.getDefault());
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millisUntilFinished);
textView.setText(formatter.format(calendar.getTime()));
here is how I am passing the values to this method, where the hours and minutes strings are "8" and "30" for example
Calendar c = Calendar.getInstance();
String s = ApplicationPreferences.getWakeUp(ActivityStage1.this);
String[] separated = s.split("\\:");
String hours = separated[0];
String minutes = separated[1];
c.add(Calendar.DAY_OF_MONTH, 1);
c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hours));
c.set(Calendar.MINUTE, Integer.parseInt(minutes));
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
long timeUntilStageTwo = (c.getTimeInMillis() - System.currentTimeMillis());
startStageTwoTimer(timeUntilStageTwo);
So the value in the long millisUntilFinished is something like 58441666, which is around 16 hours and 20 min, but for some reason the time it shows at the end in the text view is with 3 hours more, I even tried with different locales passed to SimpleDateFormat, and still the same, why is that happening?
Your Calendar c is in your local UTC+3 timezone while the system clock runs at UTC. Hence the difference of 3 hours.
You can use setTimeZone() to set an explicit timezone on your calendar before computing its millisecond value.
If you prefer to do the computations yourself, you can get the user's timezone with TimeZone.getDefault() and then use getOffset(time) to get the millisecond offset at specified UTC time.
I am using following code to convert timezone (GMT-3) to device local timezone.
int hour=17,minute=0,day=12,month=6,year=2014;
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT-3"));
cal.set(year, (month-1), day,hour,minute);
cal.setTimeZone(TimeZone.getDefault());
Log.d("Time", cal.get(Calendar.DATE)+"/"+cal.get(Calendar.MONTH)+"/"+cal.get(Calendar.YEAR)+" , "+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE)+" "+cal.get(Calendar.AM_PM));
My local timezone is GMT+5:30
Expected result is
Time 13/5/2014, 1:30 0
But I am getting the result
12/5/2014 , 13:30 1
Sorry for you, GregorianCalendar is sometimes the hell. Your problem is following:
If you immediately set the timezone after having set the fields for year, month etc. then this mutable calendar class will only shift the timezone retaining the already set fields containing the local time. Those fields for year, month etc. will NOT be recalculated. This behaviour causes a shift on the global timeline represented by cal.getTime(), too.
In order to force the calendar object to recalculate the fields you need to call a getter. Watch out for following code and especially remove the comment marks to see the effect.
int hour = 17, minute = 0, day = 12, month = 6, year = 2014;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
TimeZone tz1 = TimeZone.getTimeZone("GMT-3");
sdf.setTimeZone(tz1);
Calendar cal = new GregorianCalendar(tz1);
cal.set(year, (month - 1), day, hour, minute);
// System.out.println(sdf.format(cal.getTime()));
// System.out.println("Hour=" + cal.get(Calendar.HOUR_OF_DAY));
TimeZone tz2 = TimeZone.getTimeZone("GMT+0530");
sdf.setTimeZone(tz2);
cal.setTimeZone(tz2);
System.out.println(sdf.format(cal.getTime()));
System.out.println("Hour=" + cal.get(Calendar.HOUR_OF_DAY));
Output with comment-disabled lines:
2014-06-12T17:00+0530
Hour=17
Output with enabled lines after having removed the comment marks:
2014-06-12T17:00-0300
Hour=17
2014-06-13T01:30+0530
Hour=1
I am trying to add a list of holidays to my calendar. I am using the Caldroid library for displaying the calendar. I want to display a list of holidays in every month for which I need to select specific dates in every month. How do I do that ? The following is what I have tried:
CODE :
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -18);
Date blueDate = cal.getTime();
cal = Calendar.getInstance();
cal.add(Calendar.DATE, 16);
int diff = cal.get(Calendar.JANUARY);
cal.add(diff, 10);
Date greenDate = cal.getTime();
I believed that diff would set the month to January and highlight the 11th of January cause I have given the value as 10 but it doesn't do so and I believe it is because I have instantiated the cal to getInstance() which would return the current month.
UPDATE :
Thanks to Meno, I have achieved the following but when I set the calendar to the second time, it takes only the updates value and does not set the first date (very obvious) but I want to know how to set multiple dates in a month without re-instantiating a new GregorianCalendar object for every month. Simply put, how do I set an array of dates in a month.
GregorianCalendar greg_cal = new GregorianCalendar();
greg_cal.set(Calendar.MONTH, Calendar.JANUARY);
greg_cal.set(Calendar.DAY_OF_MONTH, 1);
greg_cal.set(Calendar.MONTH, Calendar.JANUARY);
greg_cal.set(Calendar.DAY_OF_MONTH, 18);
Thanks in advance.
Your question does not appear to be clear. Nevertheless I try an answer. Instead of
cal = Calendar.getInstance(); // In Thailand this gives the buddhist calendar, do you want this?
cal.add(Calendar.DATE, 16); // 16 days from now, what is the intention or meaning???
cal.add(diff, 10); // first argument must be a defined constant in java.util.Calendar
I assume you just want to select a fixed date (as holiday). If so then you can call the set()-method and don't need to add days to move your calendar date forth and back:
GregorianCalendar cal = new GregorianCalendar(); // including currrent year
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DAY_OF_MONTH, 11);
Then you get as date the 11th of January in current year. By the way:
int diff = cal.get(Calendar.JANUARY);
This line is nonsense because:
Calendar.JANUARY is an int constant which is zero and denotes a value (the month) not a field. But the get(int)-method expects a field constant. The field constant with value zero corresponds to Calendar.ERA. Finally the line yields the era of cal, namely int diff = GregorianCalendar.AD = 1; assuming you use the gregorian calendar. This is surely not what you want???
UPDATED because of extra question in comment:
Reusing means that you don't create a new instance for the next calculation but reuse the same one (GregorianCalendar is mutable!). For example:
GregorianCalendar cal = new GregorianCalendar(); // including currrent year
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DAY_OF_MONTH, 11);
Date holiday1 = cal.getTime();
cal.set(Calendar.MONTH, Calendar.DECEMBER); // no new instance => reuse cal
cal.set(Calendar.DAY_OF_MONTH, 24);
Date holiday2 = cal.getTime();
...
I have also written about limiting to manipulations of month and day-of-month only because with manipulation of week-related fields the state of reused Calendar-instance depends on the order of field manipulations (very ugly and surprising).
Anyway, it is always safer to use immutable types which are available in Java 8 (not useable on Android), JodaTime and my alpha-state-library. I admit that the first contact with JodaTime can cause you feeling like lost because there are so many methods (the documentation standard is good for open-source but less good than for example in JSR-310). In your use-case I would use the type org.joda.time.LocalDate as start because you really have just a plain-date-use-case. Google and SO are your friends if you want to see more documentation beyond the original Joda documentation.
UPDATE due to extended question:
You have forgotten one important thing in your new code, namely to add the results of calendar setting to a holiday list, see here the modification:
List<Date> holidays = new ArrayList<Date>();
GregorianCalendar greg_cal = new GregorianCalendar();
greg_cal.set(Calendar.MONTH, Calendar.JANUARY);
greg_cal.set(Calendar.DAY_OF_MONTH, 1);
holidays.add(greg_cal.getTime());
greg_cal.set(Calendar.MONTH, Calendar.JANUARY);
greg_cal.set(Calendar.DAY_OF_MONTH, 18);
holidays.add(greg_cal.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
for (Date d : holidays) {
System.out.println(sdf.format(d));
}
// output:
2014-01-01
2014-01-18
In an external library like JodaTime you would just use org.joda.time.LocalDate instead.
List<LocalDate> holidays = new ArrayList<LocalDate>();
LocalDate today = LocalDate.now();
holidays.add(today.withMonthOfYear(1).withDayOfMonth(1));
holidays.add(today.withMonthOfYear(1).withDayOfMonth(18));
It is pretty simple (similar in my unfinished date-and-time-library, too).
I have a Date object. Now I want to add days to that Date object.
So how that can be done? Actually using Calendar object that can be done I know.
But in my case, I haven't used a calendar objects. Instead only used date object.
For Example, suppose I have a date object
Date dtStartDate=o.getStartDate();
int x=28;
Now what I want to do is to add 28 to this date object, means if the dtStartDate is 1 July 2011
then after adding 28, dtStartDate will be 29 July 2011.
Please suggest it to me.
Thanks in advance
You can add Day using below
Calendar c1 = Calendar.getInstance();
c1.add(Calendar.DAY_OF_MONTH, 1);
Here 1 is number of Day you can add.
OR
Date dtStartDate=o.getStartDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar c = Calendar.getInstance();
c.setTime(dtStartDate);
c.add(Calendar.DATE, 3); // number of days to add
String dt = sdf.format(c.getTime()); // dt is now the new date
Toast.makeText(this, "" + dt, 5000).show();
May be your problem solved.
You could add the equivilent number of milliseconds to the time retrieved from Date, e.g.:
long millis = dtStartDate.getTime();
millis = millis + x*24*60*60*1000;
Date dtEndDate = new Date();
dtEndDate.setTime(millis);
You can easily do this in two simple ways my friend. First one is:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, 1);
and the second one is:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR_OF_DAY, 24);
I think you would like to find this thing. Though there are so many persons are there who choose the first method.
Thanks.