my dog learned polymorphism*
The moose likes Beginning Java and the fly likes trouble with date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "trouble with date" Watch "trouble with date" New topic
Author

trouble with date

kevin schmidt
Ranch Hand

Joined: Aug 24, 2001
Posts: 85
Hi,
I'm displaying information from a directory where things are dated. The program allows the user to view files from previous weeks. What I want to do is be able to display at the top of the page "News from the week of 'Monday, July 20th, 2001'", where 'Monday, July 20th, 2001' is genereated by the code. The problem is I'm not sure how to get the date so it always displays the Monday of the week, the start of the work week. I hope this is clear!! TIA.
Kevin
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I think I get it?

You are filtering the contents of a directory to show 'a week at a time'?

If you know that you want to show all files between 'day x plus 7 days' where day x is always a Monday...

Then maybe you could use a GregorianCalendar object. This is a pretty big and complicated class (or so I found), but also pretty powerful.

You can initially set it's value to BE a Monday. Henceforth, you can simply add whole weeks at a time to it, and if you add whole weeks, then the calendar's current day will always be Monday.

Am I close?
[This message has been edited by Mike Curwen (edited September 19, 2001).]
Tom Ben
Ranch Hand

Joined: Aug 17, 2001
Posts: 109
Not sure if this will help you or not. Hope it does.
import java.util.GregorianCalendar;
import java.util.Calendar;
public class Dates {
public static void main (String args[]) {
GregorianCalendar cldr = new GregorianCalendar();
int year = cldr.get(Calendar.YEAR);
int day = cldr.get(Calendar.DAY_OF_MONTH);
//String dow = dayOfWeek(cldr.get(Calendar.DAY_OF_WEEK));
//String moy = monthOfYear(cldr.get(Calendar.MONTH));
String date = dayOfWeek(cldr.get(Calendar.DAY_OF_WEEK))+", "+monthOfYear(cldr.get(Calendar.MONTH))+" "+day+", "+year;
//System.out.println(dow+", " + moy+" "+day+", "+year);
System.out.println(date);
}
public static String monthOfYear(int dayw) {
String monofyear = "";
switch (dayw) {
case 0:
monofyear = "January";
break;
case 1:
monofyear = "February";
break;
case 2:
monofyear = "March";
break;
case 3:
monofyear = "April";
break;
case 4:
monofyear = "May";
break;
case 5:
monofyear = "June";
break;
case 6:
monofyear = "July";
break;
case 7:
monofyear = "August";
break;
case 8:
monofyear = "September";
break;
case 9:
monofyear = "October";
break;
case 10:
monofyear = "November";
break;
case 11:
monofyear = "December";
break;
}
return monofyear;
}
public static String dayOfWeek(int dayw) {
String dayofweek = "";
switch (dayw) {
case 1:
dayofweek = "Sunday";
break;
case 2:
dayofweek = "Monday";
break;
case 3:
dayofweek = "Tuesday";
break;
case 4:
dayofweek = "Wednesday";
break;
case 5:
dayofweek = "Thursday";
break;
case 6:
dayofweek = "Friday";
break;
case 7:
dayofweek = "Sunday";
break;
}
return dayofweek;
}
}

------------------
Sun Certified Programmer on the Java 2 Platform


Sun Certified Programmer on the Java 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trouble with date
 
Similar Threads
How to find day from the week using date format?
daysOfWeek function implementation
date problem
Am i ready to take the exam?
Create an Array of Weeks