2

An idea of my GUI I'm making a tool that does multiple things, and I'm onto the last feature for now: You type in a date and it tells you what day of the week it would be on that date.

So I have two problems here:

  1. I can't use arguments, it doesn't like them
  2. On the last line, I can't use jTextArea2.setText(d0); because it doesn't like that either...

My code is here:

    public static void main(String[] args) { 
        int d = Integer.parseInt(args[0]);
        int m = Integer.parseInt(args[1]);
        int y = Integer.parseInt(args[2]);

        int y0 = y - (14 - m) / 12;
        int x = y0 + y0/4 - y0/100 + y0/400;
        int m0 = m + 12 * ((14 - m) / 12) - 2;
        int d0 = (d + x + (31*m0)/12) % 7;

        System.out.println("Sunday = 0\nMonday = 1\nTuesday = 2\nWednesday = 3\nThursday = 4\nFriday = 5\nSaturday = 6\nThis date is on a:");
        System.out.println(d0);

Basically, this code was at first for use with the console, and now I want to implement it into a Swing GUI app.

I'm only a week into learning Java, so excuse me if the problem is obvious or easy to fix... But can anyone figure out how to work it? Thanks!

mkhelif
  • 1,551
  • 10
  • 18
Razor Shadow
  • 65
  • 1
  • 6
  • 3
    That's a rather complex requirement for some one with only a weeks experience under their belt. I would start by having a read of [Creating a GUI With JFC/Swing](http://docs.oracle.com/javase/tutorial/uiswing/index.html) – MadProgrammer Oct 19 '12 at 04:57
  • 1
    @RazorShadow you can use `JFormattedTextField` for date values. Here is an [example](http://stackoverflow.com/q/4252257/1048330). – tenorsax Oct 19 '12 at 06:51

4 Answers4

2

The question you have at your feet, is in what format will the date come it.

You've only supplied a JTextArea for them to enter the value in, so they can enter just about anything...

So, first things first, you need a method by which you can accept an incoming value...

public String toDayOfWeek(String date) {
}

From here you need to format the incoming value to a Date

String dayOfWeek = null;
try {
    Date date = DateFormat.getDateInstance().parse(date);
} catch (ParseException exp) {
    dayOfWeek = date + " is an invalid date format";
}
return dayOfWeek;

(Obviously, the above belongs in the toDayOfWeek(String) method)

Now, personally, I would pass the Date value onto another method, but that cause I'm crazy...

public String toDayOfWeek(Date date) {
    // Now you could extract the various values from the Date object
    // but those methods are deprecated...
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    int day = cal.get(Calendar.DATE);
    int month = cal.get(Calendar.MONTH); // Months are 0 based
    int year = cal.get(Calendar.YEAR);

    // Your calculation here...

    return yourDayCalculation;
}

But to be honest, it would simpler and easier to do this...

public String toDayOfWeek(Date date) {
    // Now you could extract the various values from the Date object
    // but those methods are deprecated...
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    return DateFormatSymbols.getInstance().getWeekdays()[cal.get(Calendar.DAY_OF_WEEK)];
}

So you would end up with two methods...

public String toDayOfWeek(String date) {
    String dayOfWeek = null;
    try {
        Date date = DateFormat.getDateInstance().parse(date);
        dayOfWeek = toDayOfWeek(date);
    } catch (ParseException exp) {
        dayOfWeek = date + " is an invalid date format";
    }
    return dayOfWeek;
}

public String toDayOfWeek(Date date) {
    // Now you could extract the various values from the Date object
    // but those methods are deprecated...
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    return DateFormatSymbols.getInstance().getWeekdays()[cal.get(Calendar.DAY_OF_WEEK)];
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

You can use Calendar for it. Look at this code and you'll get the idea

Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR,2012);
cal.set(Calendar.MONTH,0);//Note that months start at 0 i.e. January is 0
cal.set(Calendar.DAY,1);

System.out.println(cal.get(Calendar.DAY_OF_WEEK)));
Abubakkar
  • 15,488
  • 8
  • 55
  • 83
  • 1
    How is that answer? At best it's a comment – MadProgrammer Oct 19 '12 at 04:58
  • @Razor Shadow `I'm only a week into learning Java, so excuse me if the problem is obvious or easy to fix...`, then how can you knows if or not – mKorbel Oct 19 '12 at 06:37
  • +1 Better `Calendar` than a custom implementation of [*Zeller's congruence*](http://en.wikipedia.org/wiki/Zeller's_congruence). See also [`Converter`](http://docs.oracle.com/javase/tutorial/uiswing/components/model.html) – trashgod Oct 19 '12 at 12:11
1

First check if there are actually 3 elements in the args by

   if(args.length == 3)
       // colculate
     else
       System.out.println("Usage: java DateID <month> <day> <year>"):
Mordechai
  • 15,437
  • 2
  • 41
  • 82
1

Assuming your GUI is not being displayed for the sake of example...

For your second problem, JTextArea inherits from JTextComponent, where you will find the setText method. Since it accepts strings and not ints, you're probably looking to do something like:

String.valueOf(d0);

I believe you could also concatenate d0 to a string literal -- such as the one in your println -- and be just as well.

Edit: Remember that setText will merely set the value of that field. If you want to get the text, you'll need to use the getText method and split the resulting string on whatever delimiters you specify. The result will be stored in an array of strings, which you could then convert to ints and use with Calendar ala Abu's response.

Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
  • What do you mean exactly? If you wanted to see my GUI, I didn't know you could post images until I just checked now. Also where do I put that piece of code exactly? – Razor Shadow Oct 19 '12 at 05:05
  • @RazorShadow Sorry -- I was referring to the code. The problematic part of the code should be relevant since you brought it up in your question... seeing it now, were you asking why setText couldn't be used as an argument? – Anthony Neace Oct 19 '12 at 05:07
  • Not quite, my problem exactly is this (sorry for being unclear if I was): It won't let me use arguments, so I need an alternative way for the user to input a day, month and year in order to receive the corresponding day of the year. Also, I need an alternative to the last line because currently it prints to the console, and if you change it to jTextArea2.setText(d0) it doesn't like it. – Razor Shadow Oct 19 '12 at 05:12
  • @RazorShadow I think I see now. Check my edit -- that should get you a to a point where you can create ints to use with Abu's response to print the day of the week. – Anthony Neace Oct 19 '12 at 05:18