Guys I need help i want to display the month and date along with the day of following 7 days in format like
Sat - 22
Sun - 23
Mon - 24
..
Using a loop
what should be my approach within the loop ::
Calendar cal = new GregorianCalendar();
for(i = 0;i<7;i++)
{
int month = cal.get(Calendar.MONTH);
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH + i); // Giving error here
day[i] = dayOfMonth + month + "";
}
I am totally a beginner in this field and im confused help me out
Changing:
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH + i);
to:
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH) + i;
Will fix the error.
But what you are trying to do can be better accomplished with:
Calendar cal = Calendar.getInstance();
for(i = 0; i < 7; i++) {
cal.add(Calendar.DAY_OF_MONTH, 1); // Adds a day to the date and takes care
// of adding month when its the last day of the month already
int weekDay = cal.get(Calendar.DAY_OF_WEEK); // Get weekday name like Sunday
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH)
day[i] = weekDay + " - " + dayOfMonth;
}
Related
My week start from Friday and end from Thursday, and i get a list of weeks of current month weeks.
In my Code every thing working fine but get current month four weeks but i want previous four weeks not next weeks of current week.
public void getWeeksOfMonth( int year) {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
Calendar cal = Calendar.getInstance();
int currentmonth = cal.get(Calendar.MONTH);
int val = 0;
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, currentmonth);
cal.set(DAY_OF_MONTH, 1);
int ndays = cal.getActualMaximum(DAY_OF_MONTH);
System.out.println(ndays + "<<<ff");
while (cal.get(DAY_OF_WEEK) != FRIDAY) {
cal.add(DAY_OF_MONTH, 1);
ndays--;
}
int remainingDays = ndays % 7;
if (remainingDays == 0)
ndays += 7;
else
ndays = ndays + 7 - remainingDays;
int inc = 1;
for (int i = 1; i <= ndays; i++) {
String day = sdf.format(cal.getTime());
System.out.println(day + "<<<");
Log.e("quest", day + "<<<");
inc++;
if (val == 0) {
firstweek = day.substring(0, 6);
// weeklist.add(firstweek);
val = 1;
}
if (i % 7 == 0) {
String s = day.substring(0, 6);
weeklist.add(firstweek + " to " + s);
val = 0;
Log.e("weekdayss", "=======week days===========" + weeklist);
inc = 0;
}
if (inc >= 1 && i == ndays) {
for (int ii = inc; ii <= 6; ii++) {
String dayi = sdf.format(cal.getTime());
System.out.println(dayi + "<<<");
Log.e("quest1", dayi + "<<<");
inc++;
}
}
cal.add(Calendar.DATE, 1);
}
if (weeklist.size() == 5) {
weeklist.remove(4);
}
if (weeklist.size() == 6) {
weeklist.remove(5);
weeklist.remove(4);
}
}
Problem
Want to get previous four weeks, not current Month four weeks
OUTPUT
[
02-Mar to 08-Mar
09-Mar to 15-Mar
16-Mar to 22-Mar
23-Mar to 29-Mar
]
A good alternative is to use the threeten backport - in Android here's how to configure it. Or, if you're using API level >= 26, just use the java.time classes.
This API makes things much easier. You can use the WeekFields class to define your week (starting on Friday):
// week starts on Friday
WeekFields wf = WeekFields.of(DayOfWeek.FRIDAY, 1);
The first parameter (DayOfWeek.FRIDAY) is the first day of the week, and the second parameter is the number of days in the first week. Check the documentation for more details about how these fields affect the class behaviour.
To get the current date, you can use the LocalDate class:
// current date
LocalDate dt = LocalDate.now();
Then you make a loop that subtracts a certain number of weeks, and use the WeekFields to get the first and last day of each week. I also used a DateTimeFormatter to print the dates to same format of your output:
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd-MMM", Locale.ENGLISH);
// get previous 4 weeks
for (int i = 4; i >= 1; i--) {
LocalDate pastWeek = dt.minusWeeks(i);
LocalDate startOfWeek = pastWeek.with(wf.dayOfWeek(), 1);
LocalDate endOfWeek = pastWeek.with(wf.dayOfWeek(), 7);
// you can add this String to your weekList
String week = startOfWeek.format(fmt) + " to " + endOfWeek.format(fmt);
System.out.println(week);
}
Output:
23-Feb to 01-Mar
02-Mar to 08-Mar
09-Mar to 15-Mar
16-Mar to 22-Mar
In my application i am displaying a calendar of days in a horizontal scrollable listview like below :
The dates are proper and the current date is also selected, the issue i am facing is the week day that is displayed.It is not proper. The code written to displayed this kind of calendar is as follows:
int count = 0;
for (int i = 1; i <= noOfDays; i++) {
int year = Calendar.YEAR;
int month = Calendar.MONTH;
int day = i;
Calendar c = new GregorianCalendar(year, month, day);
c.set(year, month, day);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String d = sdf.format(cal1.getTime());
CustomData custom = new CustomData(String.valueOf(i),
getWeekday(c.get(Calendar.DAY_OF_WEEK)), d);
mCustomData[count] = custom;
Log.e("mCustomData", d);
count++;
if(Integer.parseInt(splitDateee[0])==i)
{
currentDate = i-1;
}
}
There is an error in weekdays that is being displayed. What am i missing here? Not able to figure out the issue.
Please help ! Thanks in Advance!
int year = Calendar.YEAR;
int month = Calendar.MONTH;
These are flags belonging to calendar to get and set values, and do not indicate the current year and month. You would need to get the year and month from the current device time and do:
int year = currentYear;
int month = currentMonth;
Calling Calendar.getInstance() gets the calendar for the current day:
Calendar now = Calendar.getInstance();
You can then use the flags like so:
int month = now.get(Calendar.MONTH);
int year = now.get(Calendar.YEAR);
I am trying to get today's Year, Month and Date using following code;
Calendar calendar = Calendar.getInstance();
int thisYear = calendar.get(Calendar.YEAR);
Log.d(TAG, "# thisYear : " + thisYear);
int thisMonth = calendar.get(Calendar.MONTH);
Log.d(TAG, "# thisMonth : " + thisMonth);
int thisDay = calendar.get(Calendar.DAY_OF_MONTH);
Log.d(TAG, "$ thisDay : " + thisDay);
But it gives "2012 for year 1 for month and 28 for date" which is not today's date. What I have done wrong?
Would I be correct in assuming this is running on a Emulator? If so, Set the emulator date correctly, and it should be correct.
From memory, that code should do what you expect.
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
calendar.get(Calendar.YEAR)
import java.util.Calendar;
import java.util.TimeZone;
import android.widget.Toast;
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
int currentYear = calendar.get(Calendar.YEAR);
int currentMonth = calendar.get(Calendar.MONTH) + 1;
int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
Toast.makeText(this,"Today's Date: " + currentYear + currentMonth + currentDay, Toast.LENGTH_SHORT).show();
"TimeZone" will work great if your application targets for Android API 27 platform or above
The month starts from zero so you have to add 1 with the given month to show the month number we are familiar with.
int thisMonth = calendar.get(Calendar.MONTH);
Log.d(TAG, "# thisMonth : " + (thisMonth+1));
This will show you the current month starting with 1.
try this one..
Calendar instance = Calendar.getInstance();
currentMonth = instance.get(Calendar.MONTH);
currentYear = instance.get(Calendar.YEAR);
int month=currentMonth+1;
I tried your code and it is giving correct output. You should try checking time in your emulator/phone on which you are trying this code.
According to getInstance docs, it sets to current date and time by Default.
Try to use:
Date dt = new Date();
dt.getYear();
dt.getMonth();
dt.getDay();
and see if you get the same result.
If so, your system date is probably out of sync.
Check the Date class documentation for more details:
This gives the time and date of your android system so first check it.
You can use SimpleDateFormat
Initialising as #SuppressLint("SimpleDateFormat") final SimpleDateFormat dateAndTime = new SimpleDateFormat("DD-MM-yy", Locale.getDefault());
and String timeStamp = dateAndTime.format(new Date());
I am wanting to create a gestation period calculator for my app. I want to grab the date that the user had picked in the date time picker and advance that date by 9 months and 10 days and print it out in a textView. I am able to get the date from the date picker and print the date into the textView but what I need to do now is to advance the date by the 9 months and 10 days. Any ideas? and here is my current code for getting the date from the date picker and printing it to a text view.
public class GestationPeriod extends Activity {
DatePicker date;
TextView gestationperiodView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gestationperiod);
setupVariables();
}
public void calculate(View v){
int gestationPeriodMonth = 9;
int gestationPeriodDay = 10;
int year = date.getYear();
int month = date.getMonth();
int day = date.getDayOfMonth();
gestationperiodView.setText("" + day + " / " + month + " / " + year);
}
private void setupVariables(){
date = (DatePicker) findViewById(R.id.datePicker1);
gestationperiodView = (TextView) findViewById(R.id.editText1);
}
Your help would be much appreciated.
Use java.util.Calendar.add(int field, int amount). After getting the day, month, and year, and before gestationperiodView.setText, insert:
Calendar c = new Calendar();
c.set(year, month-1, day);
c.add(Calendar.DAY_OF_MONTH, gestationPeriodDay);
c.add(Calendar.MONTH, gestationPeriodMonth);
day = c.get(Calendar.DAY_OF_MONTH);
month = c.get(Calendar.MONTH) + 1;
year = c.get(Calendar.YEAR);
I've been trying this from a long time.
I'm having today's date and I want to compare it with specific date.
But when I set particular date like this, it gives me wrong output:
Calendar cal;
SimpleDateFormat dateformatter;
String currentdate;
cal = Calendar.getInstance();
dateformatter = new SimpleDateFormat("yyyy-MM-dd");
//Getting today's date
currentdate = dateformatter.format(cal.getTime());
Log.i(Class_Tag, "current = " + currentdate);
//Setting specific date
Date start = cal.getTime();
cal.set(2012, 06, 20);
Date end = cal.getTime();
Log.i(Class_Tag, "end = " + dateformatter.format(end));
long diff = end.getDate() - start.getDate();
Log.i(Class_Tag, "diff : " + diff);
long diff1 = end.getMonth() - start.getMonth();
Log.i(Class_Tag, "diff mnth : " + diff1);
long diff2 = end.getYear() - start.getYear();
Log.i(Class_Tag, "diff yr : " + diff2);
if (start.compareTo(end) == 0) {
Log.i(Class_Tag, "EQUAL");
} else if (start.compareTo(end) < 0) {
Log.i(Class_Tag, "end b4 start, delete");
} else {
Log.i(Class_Tag, "start b4 end, do nothing");
OUTPUT:
current = 2012-06-20
end = 2012-07-20 ////WHY it is showing month as '7' when I've set it to '6'?
diff : 0
diff mnth : 1 //Because of the wrong month in end date, this comes 1 instead of 0
diff yr : 0
end b4 start, delete //this also is not correct
I've tried several solutions, but no gains.
My only objective is to get whether that particular date has passed today's date or not.
Any help appreciated.
Edit
in the following code snippet,
//Setting specific date
Date start = cal.getTime();
cal.set(2012, 06, 20);
Date end = cal.getTime();
If I set cal.set(2012, 06, 20); to cal.set(2012, 05, 20);, the month diff. comes out to 0.
This implies that month in particular date is getting incremented by '1', but WHY?
Try this;
Calendar cal = Calendar.getInstance();
long dateNowMillis = cal.getTimeInMillis();
cal.set(2013, 1, 23);
long specDate = cal.getTimeInMillis();
if(dateNowMillis > specDate)
{
System.out.println("Passed");
}
else if(dateNowMillis == specDate)
{
System.out.println("Now");
}
else
{
System.out.println("Not passed");
}
FYI,
A month is represented by an integer from 0 to 11; 0 is January, 1 is February, and so forth; thus 11 is December.
You can check read more about Date here and more about SimpleDateFormat.
Update:
One more thing about getMonth() => Its deprecated, instead Calendar.get(Calendar.MONTH), check here.