This site requires JavaScript, please enable it in your browser!
Greenfoot back
Nosson1459
Nosson1459 wrote ...

2017/2/9

Date

1
2
Nosson1459 Nosson1459

2017/2/9

#
I'm displaying the date in a program currently like this:
1
2
3
Date today = new Date();
setText(DateFormat.getDateInstance(DateFormat.FULL).format(today));
setText(DateFormat.getTimeInstance().format(today)); // the main part is what's in the parenthesis ignore the setText which won't do anything
This code works but gives me the date all at once. I know I don't have to do DateFormat.FULL but I want all that information I just want to be able to retrieve the day of the week, the month, the date and the year all separately. I've been searching through the Java API and I couldn't figure out a simple way (or any way at all) to just get the current day of the week, month, date or year by themselves. How can I retrieve the current day, month, date, and year separately so that I can display them on different lines instead of either it saying "Wednesday, February 8, 2..." or me making the frame bigger which I don't want to do.
danpost danpost

2017/2/9

#
You could just parse the string at the commas. Then the month-day part can be parsed at the space. Or, you could do the following:
1
2
3
4
5
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK); // returns 1 (Sunday) through 7
int date = calendar.get(Calendar.DATE); // returns 1 through 31
int month = calendar.get(Calendar.MONTH); // returns 0 (January) through 11
int year = calendar.get(Calendar.YEAR);
You will then need Enum classes (or arrays) to determine the string representation of the months and days of the week.
Nosson1459 Nosson1459

2017/2/9

#
What does "You will then need Enum classes" mean? I used a switch statement block.
danpost danpost

2017/2/9

#
Nosson1459 wrote...
What does "You will then need Enum classes" mean? I used a switch statement block.
Nevermind about Enum classes -- they will not work in this situation. However, arrays will:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// with class constant array
private static final String[] DAYS =
{
    "",
    "SUNDAY",
    "MONDAY",
    "TUESDAY",
    "WEDNESDAY",
    "THURSDAY",
    "FRIDAY",
    "SATURDAY"
};
 
//you can do this:
String dayOfWeek = DAYS[day];
and do similar for the months. With months, you do not need the empty string at the beginning of the list as the int values for months start at 0 -- not 1.
Nosson1459 Nosson1459

2017/2/9

#
I didn't think of using it like that, and I started from 0 for the month. Why does it have to be "static final"?
danpost danpost

2017/2/9

#
Nosson1459 wrote...
I didn't think of using it like that, and I started from 0 for the month. Why does it have to be "static final"?
Well, the values are never going to change (final) and there is no need for each instance of the class to hold a separate copy of the array (static).
Nosson1459 Nosson1459

2017/2/10

#
So it's the same thing as the discussion we were having about private, public, protected, and restricted.
danpost danpost

2017/2/10

#
Nosson1459 wrote...
So it's the same thing as the discussion we were having about private, public, protected, and restricted.
No. Those:
private, public, protected, and restricted
are access modifiers which control what classes have access to the member (field, method or constructor) or class. The 'final' means the value cannot be changed (the compiler will complain if you tried to set the field to something else); and the 'static' means that it is, more or less, a general field -- it belongs to the class itself and each object created from the class does not get its own version of the field to work with; 'static' fields are initialized when the project is compiled and they are usable immediately (even before any objects are created from the class).
Nosson1459 Nosson1459

2017/2/10

#
danpost wrote...
Well, the values are never going to change (final) and there is no need for each instance of the class to hold a separate copy of the array (static).
Nosson1459 wrote...
So it's the same thing as the discussion we were having about private, public, protected, and restricted.
I KNOW THE DIFFERENCE BETWEEN THOSE AND THESE. What I'm saying is that you're are putting those there for the same REASON that you did in the other discussion. Technically this code won't run any differently if it didn't have "static final" but you put it there because it could be put there. (and to get in the habit of...)
danpost danpost

2017/2/10

#
Nosson1459 wrote...
Technically this code won't run any differently if it didn't have "static final".
That is not necessarily true. It may run okay without the 'static final'; but, that totally depends on when and how you access the data within the array. Without the 'static' part, you must create an object from the class and access the array through that object. Without the 'final' part, you may inadvertently try to change the array (not saying you would or would even want to).
Super_Hippo Super_Hippo

2017/2/10

#
I often use static final fields for images and some default values. It can reduce lag if each actor only has to set an image - which was drawn once earlier - rather than drawing a complicated one.
Nosson1459 Nosson1459

2017/2/10

#
danpost wrote...
Without the 'final' part, you may inadvertently try to change the array (not saying you would or would even want to).
Then see the words that I wrote:
Nosson1459 wrote...
Technically this code won't run any differently if it didn't have "static final"...
The code above won't run any differently, if I add code to change the array then it will be different but that's not this code. I know you wrote "not saying you would or would even want to" but you did say "That is not necessarily true." to "Technically this code won't run any differently if it didn't have "static final".".
Nosson1459 Nosson1459

2017/2/10

#
Thanks for the help on the Calendar class (and discussion on final).
Super_Hippo Super_Hippo

2017/2/10

#
Trying to get every detail correct, you could say that the result is the same for you although the code itself is in fact running differently (at least static vs. non-static, I don't know how much final is different than non-final in terms of "running").
danpost danpost

2017/2/10

#
Super_Hippo wrote...
I don't know how much final is different than non-final in terms of "running").
I do not believe that there is any difference as far as "running". However, you will get a compilation error if you try to assign a new value (or reference) to a 'final' field. @Nosson1459, you stated this:
Technically this code won't run any differently if it didn't have "static final"
which put 'static' in the "equation". That was what prompted my:
That is not necessarily true.
There are more replies on the next page.
1
2