Calendar view in android should display 1 week from current date - java

I am trying to get the CalendarView widget to display only 1 week from current date. I tried using ,
Date date = new Date();
cal = (CalendarView) findViewById(R.id.calendarView1);
cal.setMaxDate(date.getTime()+604800000);
cal.setMinDate(date.getTime());
but it doesn't seem to work. Really appreciate if someone could help me out.

u can set like this here eg. i set the date to 25 -12(december)- current year. You want something like: just get 1 week from current date and u can set like this also
public static Calendar defaultCalendar() {
Calendar currentDate = Calendar.getInstance();
currentDate.set(Calendar.MONTH, 11); // Months are 0-based!
currentDate.set(Calendar.DAY_OF_MONTH, 25); // Clearer than DATE
return currentDate;
}

Related

Increment in date choosen from JDateChooser [duplicate]

This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
Closed 4 years ago.
I am stuck in a programming issue. I am am making Java Application in Eclipse attached with SQlite. I want such state where user choose a date from JDateChooser and date of after two days from the specified one in JDateChooser show into next field.
I am using the following code which I know works..
DateFormat df=new SimpleDateFormat("dd-MM-yyyy");
Date date = df.parse(ArrivalDate);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, 2); // Add 30 days
Date futureDate = cal.getTime();
String NextDate=df.format(futureDate);
----Here ArrivalDate is date entered by user.
Issue over here is this, that Calender class choose the present date of the day. Not work with the date choose by the user.
For example, if today date is 01/08/2018 and user has entered 12/08/2018.. This code will give 03/08/2018 in return, not the 14/08/2018. How do I achieve this scenario?? Kindly help.
Using LocalDate and DateTimeFormmatter instead of Date and Calendar can be done like this:
public static void main(String[] args) {
String arrivalDate = "12/08/2018";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate arrival = LocalDate.parse(arrivalDate, formatter);
LocalDate futureDay = arrival.plusDays(2);
String nextDate = futureDay.format(formatter);
System.out.println("Arrival: " + arrivalDate);
System.out.println("Next Day: " + nextDate);
}
The output:
Arrival: 12/08/2018
Next Day: 14/08/2018

Why this adb logcat time won't display the year

The output in all the samples i've seen is always like
08-16 10:34:13.280 D/MobileDataStateTracker( 886): default: This tracker is connected to sub=0
The date and time displaying without a year,
the format is always MM-dd HH:mm:ss.SSS
Can I change the format to get the year as well?
For now I use a workaround like this:
public static Date addCurrentYearToDate(Date extractDate) {
Calendar instance = Calendar.getInstance();
instance.setTime(extractDate);
instance.add(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR) - instance.get(Calendar.YEAR));
Date actualDate = new Date(instance.getTimeInMillis());
return actualDate;
}

code for removing date or week or month from today date

I need to get the date based on the following condition,
From the current date, If the user selects only date(1-31), must subtract it from the current date and get the new date.
If the user selects weeks(1-53) then we have to remove the selected no'of weeks from the current dates no'of weeks and get a new date.
If the user selects only months(1-12) then we have to delete the selected number of months from the current date months and get a new date.
Sample code for date checking..
Calendar today=Calendar.getInstance();
int month=today.MONTH;
int year=today.YEAR;
today.clear();
today.set(year, month,dateOfMonth);
date=today.getTime();
Any Logic is highly appreciated.
Thanks in Advance.
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTime(currentDate);
if (inDays) {
cal.add(Calendar.DATE, -n);
} else if (inWeeks) {
cal.add(Calendar.WEEK_OF_YEAR, -n);
} else if (inMonths) {
cal.add(Calendar.MONTH, -n);
}
You can achieve all of this using :
java.util.Calendar.add(int, int)
For instance:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -10); // substracts 10 days from 'now'
Use JodaTime.
Add days to a date:
http://joda-time.sourceforge.net/apidocs/org/joda/time/Days.html#plus(int)

How to get calendar from date

I have some date and I want to get last x day before this date so this is my code:
Calendar today = Calendar.getInstance();
today.add(Calendar.DATE, -x);
date = new Date(today.getTimeInMillis()))
this code works only if some day is actual day. How I can change it. Is there some method to get calendar from date ?
Use the setTime method to set the date of your calendar :
Calendar aDay = Calendar.getInstance();
aDay.setTime(aDate);

Date text box should be always 1st date current month.?

I want a javascript or java program should always give date 1st of current month.
Is there any tech?
You can use Calendar for Java
Date date = new Date(System.currentTimeMillis());
cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1);
Now you do what every you want to do with this Calendar object like to get the Day of the Week (Sat, Sun, .... )
int weekday = cal.get(Calendar.DAY_OF_WEEK);
And for JavaScript you can use:
var theFirst = new Date();
theFirst.setDate(1);
setDate sets the day of the month for the Date object (from 1 to 31). Then you can do whatever you want with theFirst, like get the day of the week.
Calendar ans = Calendar.getInstance();
ans.set(ans.get(Calendar.YEAR),
ans.get(Calendar.MONTH),
1,
0,
0,
0
);
System.out.println(ans.getTime());

Categories