Project Euler #19 in Java - java

You are given the following information, but you may prefer to do some research for yourself.
1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
Solution :
My following logic gives me 173 Sundays , whereas there are 171 Sundays !! Where are the extra 2 days came from ?
public static void main(String args[]) {
Date startDate = new Date(1901, Calendar.JANUARY, 01);
Date endDate = new Date(2000, Calendar.DECEMBER, 31);
checkSundays(startDate, endDate);
}
private static void checkSundays(Date start, Date end) {
int dayNum;
Calendar startDate = Calendar.getInstance();
startDate.setTime(start);
System.out.println(startDate.getTime());
Calendar endDate = Calendar.getInstance();
endDate.setTime(end);
System.out.println(endDate.getTime());
int count = 0;
while (startDate.before(endDate)) {
for (int i = 1; i < 13; i++) {
dayNum = startDate.get(Calendar.DAY_OF_WEEK);
if (dayNum == 1) {
count++;
}
System.out.println(startDate.getTime());
startDate.add(Calendar.MONTH, 1);
}
System.out.println("Count " + count);
}
}

Your following code uses deprecated constructor of Date
Date startDate = new Date(1901, Calendar.JANUARY, 01);
System.out.println(startDate);
which is not proper, it prints
Thu Jan 01 00:00:00 IST 3801
So use Calendar to construct Date,
Calendar startDateCal = createDateInstance(0,1901,1)
Calendar endDateCal = createDateInstance(11,2000,13)
and a Factory method
public static Date createDateInstance(int month, int year, int date){
Calendar cal= Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DATE, date);
return cal.getTime();
}
See your working code

You can use probability and do this with just a calculator.
There's 100 years in a century, and every year, there are 12 first days of the months. Divide that by 7 and you have your answer.
It's cheap, but it works.

Related

Current month count monday?

How do you find the total number of Mondays (e.g. 4 or 5) in a particular month ???
Calendar c = Calendar.getInstance();
int mon = c.getActualMaximum(Calendar.MONDAY);
is this right way ??
you can use this method
public int countMonday(int year, int month) {
Calendar calendar = Calendar.getInstance();
// Note that month is 0-based in calendar, bizarrely.
calendar.set(year, month - 1, 1);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int count = 0;
for (int day = 1; day <= daysInMonth; day++) {
calendar.set(year, month - 1, day);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.MONDAY) {
count++;
// Or do whatever you need to with the result.
}
}
return count;
}
Updated
public int countDayOccurence(int year, int month,int dayToFindCount) {
Calendar calendar = Calendar.getInstance();
// Note that month is 0-based in calendar, bizarrely.
calendar.set(year, month - 1, 1);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int count = 0;
for (int day = 1; day <= daysInMonth; day++) {
calendar.set(year, month - 1, day);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == dayToFindCount) {
count++;
// Or do whatever you need to with the result.
}
}
return count;
}
And then you can call this method for each day name
int countMonday = countDayOccurence(year,month,Calendar.MONDAY);
int countTuesday = countDayOccurence(year,month,Calendar.TUESDAY);
...............................................
Using the Calendar api, the best option I can see is to:
get the actual maximum day of month (i.e. how many days in this month)
set calendar to first day of the month and get the day of the week
calculate how many mondays could occur (i.e. if 28 days in month - 4, if 29 4 unless month started on a monday, if 30, 4 unless month started on monday or tuesday, if 31, 4 unless started on monday, tuesday, or wednesday).
Here is what you need at least for the current month :
Calendar c = Calendar.getInstance();
int maxDaysInMonth = c.getMaximum(Calendar.DAY_OF_MONTH);
int firstMonday = c.get(Calendar.MONDAY); // first monday in the month (Beware, 0 is the first day of the month)
int mondays = 0;
int i=firstMonday;
while(i<maxDaysInMonth){
mondays++;
i+=7;
};
System.out.println(mondays);
As per my question.
#Ramzan Zafar:
Answer is right and more in that i created my calender instance for current year and month for year and month.
I got my answer as per my question.

Setting the date's day / month / year to default, only care about time

I have this Date date input variable, how can I set date's day to 1, month to 1, and year to 1970? I need to do this because this date is being checked if it is included in a range of Date type, and the range is is checking against has the default date, 1-1-1970. So I want to just scrap the month, day, and year part of the date completely and only check the time to see if is included in the range. By setting them input date's to 1-1-1970 will bypass that problem.
You can do it using Calendar:
Calendar calendar = Calendar.getInstance();
calendar.setTime(yourDate);
calendar.set(Calendar.YEAR, 1970);
calendar.set(Calendar.MONTH, Calendar.JANUARY);
calendar.set(Calendar.DATE, 1);
yourDate = calendar.getTime();
If I understand your requirement, one solution would use a Calendar -
public static Date getDate(int hh, int mm, int ss) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 1970);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR, hh);
cal.set(Calendar.MINUTE, mm);
cal.set(Calendar.SECOND, ss);
return cal.getTime();
}
// Another approach; using epoch.
public static Date getDate2(int hh, int mm, int ss) {
long epoch = new GregorianCalendar(1970, 0, 1).getTimeInMillis();
long v = (ss * 1000) + (mm * 60 * 1000) + (hh * 60 * 60 * 1000);
return new Date(epoch + v);
}
public static void main(String args[]) {
Date a = getDate(1, 2, 3);
Date b = getDate2(1, 2, 3);
// Do we get the "same" Date?
System.out.println(a.toString().equals(b.toString()));
}
Output is
true

Get Week Dates From Monday to Friday from a Given Date [duplicate]

This question already has answers here:
get week start and end date from week number & year in android
(2 answers)
Closed 10 years ago.
I want to get the Week Dates from Monday to Friday From a Given Date.
Suppose if the Given Date is 6-2-2013
I want the result :
Monday 4-2-2013
Tuesday 5-2-2013
Wednesday 6-2-2013
Thursday 7-2-2013
Friday 8-2-2013
or Even i have the Week no as 6 ...How can i achieve the About result
I get the Week no from the Below Code
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, 6);
c.set(Calendar.MONTH, 1);
c.set(Calendar.YEAR, 2013);
int weekNo = c.get(Calendar.WEEK_OF_YEAR);
Try this...I have edited and added more changes to Achintya Jha Code...Works Fine
Thanks to Achintya Jha
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, 14);
c.set(Calendar.MONTH, 1);
c.set(Calendar.YEAR, 2013);
int weekNo = c.get(Calendar.WEEK_OF_YEAR);
//Set the Week No
c.set(Calendar.WEEK_OF_YEAR, weekNo);
c.clear();
//Set the Week Year and the Week No
c.set(Calendar.WEEK_OF_YEAR, weekNo);
c.set(Calendar.YEAR, 2013);
SimpleDateFormat formatter = new SimpleDateFormat("EEE dd/MM/yyyy"); // PST`
//Get Week Start Date
Date startDate = c.getTime();
//Now it will be sunday so add plus one so now it becomes Monday
c.add(Calendar.DATE, 1);
for (int i = 0; i < 5; i++) {
//From Monday to Friday Dates will be Printed
System.out.println(formatter.format(c.getTime()));
c.add(Calendar.DATE, 1);
}
From 6-2-2013:
GregorianCalendar c = new GregorianCalendar(2013, 1, 6);
c.add(Calendar.DAY_OF_YEAR, Calendar.MONDAY - c.get(Calendar.DAY_OF_WEEK));
DateFormat df = new SimpleDateFormat("d-M-yyyy");
while(c.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
if (c.get(Calendar.YEAR) == 2013) {
System.out.printf("%-10tA%10s%n", c.getTime(), df.format(c.getTime()));
}
c.add(Calendar.DAY_OF_YEAR, 1);
}
prints
Monday 4-2-2013
Tuesday 5-2-2013
Wednesday 6-2-2013
Thursday 7-2-2013
Friday 8-2-2013
from week 6 of 2013:
GregorianCalendar c = new GregorianCalendar(2013, 0, 1);
c.add(Calendar.WEEK_OF_YEAR, 6 - 1);
c.add(Calendar.DAY_OF_YEAR, Calendar.MONDAY - c.get(Calendar.DAY_OF_WEEK));
DateFormat df = new SimpleDateFormat("d-M-yyyy");
while(c.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
if (c.get(Calendar.YEAR) == 2013) {
System.out.printf("%-10tA%10s%n", c.getTime(), df.format(c.getTime()));
}
c.add(Calendar.DAY_OF_YEAR, 1);
}
prints
Monday 4-2-2013
Tuesday 5-2-2013
Wednesday 6-2-2013
Thursday 7-2-2013
Friday 8-2-2013
public static void main(String[] args) {
getWeekOfDates(2013, 2, 11);
}
private static void getWeekOfDates(int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
int weekOfMonth = c.get(Calendar.WEEK_OF_MONTH);
c.set(Calendar.WEEK_OF_MONTH, weekOfMonth);
DateFormat df = new SimpleDateFormat("EEE dd-MM-yyyy");
for (int i = 0; i < 5; i++) {
System.out.println(df.format(c.getTime()));
c.add(Calendar.DATE, 1);
}
}
Try this: I edited as your requirement.
Output:
Mon 11-03-2013
Tue 12-03-2013
Wed 13-03-2013
Thu 14-03-2013
Fri 15-03-2013

java - to calculate number of weeks - with start date as MONDAY and end date as SUNDAY

Following is the code I used to calculate the number of weeks in a Month. But actually I need number of weeks with each week's start day as MONDAY and end day as SUNDAY. For example, JANUARY, 2012 will have 5 weeks. But with the above criteria, it will have 6 weeks.
January 2012
first week - Sunday 01
second week - 2 Mon to 8 Sunday
Third week - 9 Monday to 15 Sunday
fourth week - 16 Mon to 22 Sunday
fifth week - 23 Mon to 29 Sunday
Sixth week - 30 Monday to 31 Tuesday.
The following code gives only 5 weeks.
public class Test {
public static void main(String[] args)
{
Calendar calendar = Calendar.getInstance();
int year = 2012;
int month = Calendar.JANUARY;
int date = 1;
calendar.set(year, month, date);
int numOfDaysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int numOfWeeksInMonth = calendar.getActualMaximum(Calendar.WEEK_OF_MONTH);
System.out.println("Number of Days In Month: " + numOfDaysInMonth);
System.out.println("Number of Weeks In Month: " + numOfWeeksInMonth);
}
}
The output for the above code is
Number of Days In Month: 31
Number of Weeks In Month: 5
But I need to get the "Number of Weeks In Month:" as 6
*Also I am trying to get the start date and the end date of each week in ddMMYYYY format.. *
I am still working on it.
Can anyone please help me in fixing this?
Add this to get "Number of Weeks In Month:" as 6,
calendar.setFirstDayOfWeek(Calendar.MONDAY);
try this function
public static void main(String[] args) {
System.out.println(getNumberOfWeeks(2012, Calendar.JANUARY));
}
static int getNumberOfWeeks(int year, int month) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, 1);
int numOfWeeksInMonth = 1;
while (c.get(Calendar.MONTH) == month) {
c.add(Calendar.DAY_OF_MONTH, 1);
if (c.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
numOfWeeksInMonth++;
}
}
return numOfWeeksInMonth;
}
try this:
public static int noWeeks(int year,int month)
{
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, 1);
int initDay = c.get(Calendar.DAY_OF_WEEK)-1
;
int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
int a = (initDay==0?7:initDay)+days-1;
return a/7+(a%7==0?0:1);
}
O algoritmo correto é esse:
public static int getTotalWeeksOfMonth(int month, int year){
int monthIndex = month-1;
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, monthIndex);
c.set(Calendar.DAY_OF_MONTH, 1);
int numOfWeeksInMonth = 0;
while (c.get(Calendar.MONTH) == monthIndex) {
c.add(Calendar.DAY_OF_MONTH, 1);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
if ( dayOfWeek == Calendar.SUNDAY) {
numOfWeeksInMonth++;
}
}
if (c.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
numOfWeeksInMonth++;
}
return numOfWeeksInMonth;
}

How to get Calendar.MONTH from a given WEEK_OF_YEAR, DAY_OF_WEEK,YEAR

Knowing the year, week of year and day of week is it possible to obtain the month of year and the day of month. For example
// corresponding to September 15, 2012 if week starts on Monday
int weekNum = 38;
int dayNum = 6;
int year = 2012;
// set the calendar instance the a week of year and day in the future
Calendar aGMTCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
aGMTCalendar.setFirstDayOfWeek(Calendar.MONDAY);
aGMTCalendar.set(Calendar.WEEK_OF_YEAR,weekNum );
aGMTCalendar.set(Calendar.DAY_OF_WEEK,dayNum );
aGMTCalendar.set(Calendar.YEAR,year);
// get the month and day of month
int monthGMT = aGMTCalendar.get(Calendar.MONTH + 1); // returns 38 not 9
int dayOfMonthNumGMT = aGMTCalendar.get(Calendar.DAY_OF_MONTH);
// returns 14 but I wanted 15
Thank you
This should be
// +1 to the value of month returned, not to the value of MONTH constant.
int monthGMT = aGMTCalendar.get(Calendar.MONTH) + 1;
The way you obtain the monthGMT has a type. It should be:
int monthGMT = aGMTCalendar.get(Calendar.MONTH) + 1;
Put the line below after each aGMTCalendar.set() call and you will see that after calling the dayNum one, the date changes from 15 to 14. The aGMTCalendar.set(Calendar.DAY_OF_WEEK, dayNum) ignores the setFirstDayOfWeek, which is however considered when setting the WEEK_OF_YEAR.
System.out.println(aGMTCalendar.getTime());
// corresponding to September 15, 2012 if week starts on Monday
int weekNum = 38;
int dayNum = 6;
int year = 2012;
// set the calendar instance the a week of year and day in the future
Calendar aGMTCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
aGMTCalendar.setFirstDayOfWeek(Calendar.MONDAY);
aGMTCalendar.set(Calendar.WEEK_OF_YEAR,weekNum );
aGMTCalendar.set(Calendar.DAY_OF_WEEK,dayNum );
aGMTCalendar.set(Calendar.YEAR,year);
// get the month and day of month
int monthGMT = aGMTCalendar.get(Calendar.MONTH) + 1;
// should be 10
int dayOfMonthNumGMT = aGMTCalendar.get(Calendar.DAY_OF_MONTH) + 1;
// should be 15
Try Calendar.SATURDAY constant instead of 6 literal.
Calendar.SATURDAY is in fact 7 not 6.

Categories

Resources