Results 1 to 8 of 8
Thread: Calendar in java  problem
 01042010, 05:14 PM #1Member
 Join Date
 Jan 2010
 Posts
 10
 Rep Power
 0
Calendar in java  problem
Hi,
I have a question... I have program which counts days between different dates but I would like to change:
"public final static String MONDAY = "MONDAY"" to "public static enum MONDAY("monday",1) etc, but I don't really know how to use enumerations.
Java Code:import javax.swing.JOptionPane; public class Day { public final static String MONDAY = "MONDAY"; public final static String TUESDAY = "TUESDAY"; public final static String WENDSDAY = "WENDSDAY"; public final static String THURSDAY = "THURSDAY"; public final static String FRIDAY = "FRIDAY"; public final static String SATURDAY = "SATURDAY"; public final static String SUNDAY = "SUNDAY"; public final static String JANUARY = "JANUARY"; public final static String FEBRUARY = "FEBRAURY"; public final static String MARCH = "MARCH"; public final static String APRIL = "APRIL"; public final static String MAY = "MAY"; public final static String JUNE = "JUNE"; public final static String JULY = "JULY"; public final static String AUGUST = "AUGUST"; public final static String SEPTEMBER = "SEPTEMBER"; public final static String OCTOBER = "OCTOBER"; public final static String NOVEMBER = "NOVEMBER"; public final static String DECEMBER = "DECEMBER"; public final static String WINTER = "WINTER"; public final static String SUMMER = "SUMMER"; public final static String AUTUMN = "AUTUMN"; public final static String SPRING = "SPRING"; public final static String WINTER_TERM = "WINTER TERM"; public final static String SUMMER_TERM = "SUMMER TERM"; public final static String HOLIDAYS = "HOLIDAYS"; public final static String WINTER_EXAMS = "EXAMS"; public final static String SUMMER_EXAMS = "EXAMS"; public final static Day TODAY = new Day(22, 12, 1500, Day.TUESDAY); int day; int month; int year; int dayOfYear; int dayOfMonth; int weekOfYear; String dayOfWeek; String season=season(); int numberOfDaysSinceTheBeginningOfTheEpoch; //constructors public Day(int numberOfDaysSinceTheBeginningOfTheEpoch) { //since 15.10.1582 this.numberOfDaysSinceTheBeginningOfTheEpoch = numberOfDaysSinceTheBeginningOfTheEpoch; } public Day(int dayOfYear, int year) { this.year = year; this.dayOfYear = dayOfYear; } public Day(String dayOfWeek, int weekOfYear, int year) { this.dayOfWeek = dayOfWeek; this.weekOfYear = weekOfYear; this.year = year; } public Day(int dayOfMonth, int month, int year) { day = dayOfMonth; this.month = month; this.year = year; } private Day(int day, int month, int year, String dayOfWeek) { if (month>12) { JOptionPane.showMessageDialog(null,"Month number cannot be greater then 12!"); System.exit(0); } this.month = month; this.day = day; this.year = year; this.dayOfWeek = dayOfWeek; } //method int dayOfYear() { int result = 31 * month  day; if (month>=2) result=result3; if (month>=4) result=result4; if (month>=6) result=result5; if (month>=9) result=result6; if (month>=11) result=result7; if (isLeapYear()&&month>=2) result++; return result; } int daysLeft() { int result = 31 * month  day; if (month>=2) result=result3; if (month>=4) result=result4; if (month>=6) result=result5; if (month>=9) result=result6; if (month>=11) result=result7; if (isLeapYear()) result++; if (isLeapYear()) result=366result; else result=365result; return result; } int weekOfYear() { int result = 31 * month  day; if (month>=2) result=result3; if (month>=4) result=result4; if (month>=6) result=result5; if (month>=9) result=result6; if (month>=11) result=result7; if (isLeapYear()) result++; result=result/7; return result; } int weeksLeft () { int result = 31 * month  day; if (month>=2) result=result3; if (month>=4) result=result4; if (month>=6) result=result5; if (month>=9) result=result6; if (month>=11) result=result7; if (isLeapYear()) result++; result=result/7; result=52result; return result; } public int monthsLeft() { int result=12month; return result; } public String month() { String result=""; if (month==1) result= JANUARY; if (month==2) result= FEBRUARY; if (month==3) result= MARCH; if (month==4) result= APRIL; if (month==5) result= MAY; if (month==6) result= JUNE; if (month==7) result= JULY; if (month==8) result= AUGUST; if (month==9) result= SEPTEMBER; if (month==10) result= OCTOBER; if (month==11) result= NOVEMBER; if (month==12) result= DECEMBER; return result; } public String toString() { String result=""; if (day!=0) result = result+day; if (day==1) result = result + "st "; if (day==2) result = result + "nd "; if (day==3) result = result + "rd "; if (day>3) result = result + "th "; if (dayOfMonth!=0) result = result+dayOfMonth+" "; if (month!=0) result = result+month()+" "; if (year!=0) result = result+year+" "; if (dayOfWeek!=null) result=result+dayOfWeek+" "; if (dayOfYear!=0) result=result+"Day of year: "+dayOfYear+" "; if (weekOfYear!=0) result=result+"Week of year: "+weekOfYear+" "; return result; } public boolean isLeapYear() { if (((year%4==0) && (year%100!=0))  (year%400==0)) return true; else return false; } public String season() { /* Spring: 21 march 22 june Summer: 22 june 23 september Autumn: 23 september 22 december Winter: 22 december 21 march */ String result=""; if ((day>=21 && month>=3)  (day<=22 && month<=6)) result=SPRING; if ((day>=22 && month>=6)  (day<=23 && month<=9)) result=SUMMER; if ((day>=23& month>=9)  (day<=22&& month<=12)) result=AUTUMN; if ((day>=22 && month>=12)  (day<=21 && month<=3)) result=WINTER; return result; } public int year() { return year; } public String academicSeason() { /* winter term: 30 septmeber  30 january summer term: 1 march  21 june holidays 2 july  30 september winter exams 1 febraury  13 febraury summer exams 22 june  2 july */ String result=""; if ((day>=30 && month>=9)  (day<=30 && month<=1)) result=WINTER_TERM; if ((day>=1 && month>=3)  (day<=21 && month<=6)) result=SUMMER_TERM; if ((day>=2& month>=7)  (day<=30&& month<=9)) result=HOLIDAYS; if ((day>=1 && month>=2)  (day<=13 && month<=2)) result=WINTER_EXAMS; if ((day>=22 && month>=6)  (day<=2 && month<=7)) result=SUMMER_EXAMS; return result; } public String dayOfWeek() { String result=""; int M = (1 + (month + 9) % 12) ; if (M>10) year; int C = (year / 100); int D = (year % 100); int dow = ((((13*M1) / 5) + D + (D / 4) + (C / 4) + (5*C) + day) % 7) ; if (dow==0) result=MONDAY; if (dow==1) result=TUESDAY; if (dow==2) result=WENDSDAY; if (dow==3) result=THURSDAY; if (dow==4) result=FRIDAY; if (dow==5) result=SATURDAY; if (dow==6) result=SUNDAY; return result; } public int daysBetween(Day day) { int result = 0; int leap = 0; int year1 = year; int year2 = day.year; for (int i=(year1); i<(year2);i++) { if (((i%4==0) && (i%100!=0))  (i%400==0)) leap++; } if (year1==year2) { result=day.dayOfYear()dayOfYear(); } else result=((year2year1)*365)+leap(dayOfYear()day.dayOfYear()); if (result<0) result=result*(1); return result; } public static void main(String[] args) { System.out.println("Today is: "+TODAY); int a = ((16+30+31)+((20091582)*365))  9; Day daysEpoch = new Day(a); System.out.println("Number of days since 15.10.1582: "+daysEpoch.numberOfDaysSinceTheBeginningOfTheEpoch); System.out.println("The season is: "+TODAY.season()); System.out.println(); Day b1 = new Day(2, 10, 1990); Day b2 = new Day(15, 8, 2012); Day b3 = new Day(23, 4, 3040); System.out.println(b1+" is "+b1.dayOfWeek()); System.out.println(b1.month()); System.out.println("Academic season: "+b1.academicSeason()); System.out.println(); System.out.println(b2); System.out.println("Season: "+b2.season()); System.out.println("Week of year: "+b2.weekOfYear()); System.out.println("Weeks left: "+b2.weeksLeft()); System.out.println(); System.out.println(b3); System.out.println("Day of year: "+b3.dayOfYear()); System.out.println("Days left: "+b3.daysLeft()); System.out.println(); Day c1 = new Day(241, 2007); System.out.println(c1); System.out.println(c1.year()); System.out.println(); Day d1 = new Day(MONDAY, 5, 2001); System.out.println(d1); System.out.println(); Day e1 = new Day(24, 7, 2008); Day e2 = new Day(24, 7, 2009); System.out.println("Days between "+e1+" and "+e2+"= "+e1.daysBetween(e2)); } }
 I have a question... I have program which counts days between different dates but I would like to change:
"public final static String MONDAY = "MONDAY"" to "public static enum MONDAY("monday",1) etc, but I don't really know how to use enumerations.
edit: and you'll not want to create a new enum for each week day but rather one that encompasses the whole week  but the tutorial will explain all this.Last edited by Fubarable; 01042010 at 05:51 PM.
 01042010, 06:15 PM #3Member
 Join Date
 Jan 2010
 Posts
 10
 Rep Power
 0
I have tried but I am not able to implement this methods in my program. Could you help?

 01042010, 06:43 PM #5Member
 Join Date
 Jan 2010
 Posts
 10
 Rep Power
 0
The deadline is tomorrow so I really count on your help... For you it is few minutes and for me hours :/ If you show me how to implement enums in my code for example only in days I will do it my own.

Again, I will help if possible, but we first have to know what your problems are. Again, you must first show us your "good faith" effort at solving this. Again, there's a decent example in the tutorial linked above that shows how to implement enums and in fact (again) uses days of the week as the example. If you don't understand the tutorial, at least tell us what in the tutorial and its example that confuses you. Otherwise no one will know how to help you.
I'm not sure what you are looking for here, but please know that this is not a code mill. We're not going to do your homework for you, but we'll be more than happy to help you, if you'll only let us.
Much luck.Last edited by Fubarable; 01042010 at 08:48 PM.
 01042010, 08:19 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28

Agree, no one likes to feel pressured, least of all a volunteer. I think that JavaRanch says it best here: EaseUp
Other similar links appropriate to this thread include:
ShowSomeEffort
PatienceIsAVirtue
NotACodeMill
DoYourOwnHomework
Similar Threads

Java Calendar Planner
By Alex04 in forum New To JavaReplies: 0Last Post: 09182009, 02:44 PM 
Calendar  problem in Cprompt
By pinkdreammsss in forum New To JavaReplies: 8Last Post: 04142009, 01:09 PM 
Calendar language problem
By kopros in forum New To JavaReplies: 1Last Post: 10272008, 10:00 PM 
Java Calendar Class
By vasu18 in forum New To JavaReplies: 1Last Post: 12222007, 02:51 AM 
Problem with calendar
By Felissa in forum Advanced JavaReplies: 2Last Post: 07012007, 08:39 PM
Bookmarks