Java METHOD to return date instead of Calendar field
I'm working with IBM WebSphere Host-on-Demand which is a IBM java applet. It has a built in macro builder which allows for a basic XML script to be built and allows for Java classes to be imported and called, but only methods within those classes are recognized.
This is a problem because Calendar.DATE is a field and not a method so it throws an exception because the macro is expecting a method syntax (such as java.util.DATE() which was replaced by Calendar.DAY_OF_MONTH or Calandar.DATE after verson 1.1).
Is there an actual method that will return a date? Specifically what I need is the day of the month so I can subtract 1 and get yesterdays date.
Thanks.
Re: Java METHOD to return date instead of Calendar field
I may need to be a little more clear.
My specific issue is with the Calendar.DATE and Calendar.DAY_OF_MONTH because in order to get yesterdays date I would have to do something like:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE,-1);
but the script doesn't recognize Calendar.DATE because it isn't a method.
Re: Java METHOD to return date instead of Calendar field
Quick and dirty solution.
Write a method that returns the value of Calendar.DATE.
cal.add(getCalendar_DATE(), -1);
Re: Java METHOD to return date instead of Calendar field
You might consider using JodaTime instead of the standard Date/Calendar classes. It is a lot simpler and easier to use, and has far fewer static 'enum' fields.
In fact, there was a plan to revise Java 7 date handling based on JodaTime (JSR 310), although I don't think it got anywhere...
Time is an excellent teacher; but eventually it kills all its students...
Anon.
Re: Java METHOD to return date instead of Calendar field
Quote:
Originally Posted by
Norm
Quick and dirty solution.
Write a method that returns the value of Calendar.DATE.
cal.add(getCalendar_DATE(), -1);
The only way this would work is if I was able to put the class where the program could find it (which would have to be the class path where all the other java classes are found). Does anyone know how to do that?
Re: Java METHOD to return date instead of Calendar field
Sorry, I'm not familiar with your environment.
What class are you trying to have the program (what program) find?
Re: Java METHOD to return date instead of Calendar field
Quote:
Originally Posted by
Malisk
I'm working with IBM WebSphere Host-on-Demand which is a IBM java applet. It has a built in macro builder which allows for a basic XML script to be built and allows for Java classes to be imported and called, but only methods within those classes are recognized
Host-On-Demand is the program (Specifically a java applet that begins after signing in to the clients Host-On-Demand server through the web browser).
The class would be the one you mentioned above (writing my own method within a custom class to find the date).
If I wanted to import a class in Host-On-Demand though, I would have to create the class and then put it wherever the java API classes are because thats the only place Host-On-Demand looks for classes. Is that something that can be done?
Re: Java METHOD to return date instead of Calendar field
Quote:
place the program looks for classes
What program? The only one that "looks for classes" is the jvm.
The java program looks for classes on the classpath. If the applet is in a jar file, then the java program will look in that jar file for classes that are referenced by the main/applet class.
Quote:
wanted to import a class
Does this have anything to do with the import statement?
Or does your program have a classloader that loads classes or uses reflection.
Re: Java METHOD to return date instead of Calendar field
Quote:
Originally Posted by
Norm
What program? The only one that "looks for classes" is the jvm.
The java program looks for classes on the classpath. If the applet is in a jar file, then the java program will look in that jar file for classes that are referenced by the main/applet class.
OK, you're right, I was confusing myself I guess. The program is just a java applet and therefore the program I'm referring to then is the jvm, therefore I must place any custom class in my classpath. So my question is, how do I find out what my classpath is for java?
Quote:
Originally Posted by
Norm
Does this have anything to do with the import statement?
Or does your program have a classloader that loads classes or uses reflection.
It must have a class loader, I can import java namespaces in the script and then use their methods but only if those namespaces are in the Java API. That is what I'm trying to get around though, I need a way to create my own class or namespace and place it in the same location as the Java API (which would be my classpath I guess) so that the script (running in Java) may find it.
Heres some documentation on what I'm working with (look under "Variables and imported Java classes").
http://publib.boulder.ibm.com/infoce...o%2Fmacro.html
thanks for your help btw.
Re: Java METHOD to return date instead of Calendar field
Quote:
I can import java namespaces in the script and then use their methods
Some vocabulary/concept problems here.
The import statement tells the compiler where to look to find class definitions if the classes are in packages (sort of like namespaces). I assume you mean the java source when you say script.
The program source defines new instances of classes (the javac command looks up their definition). With an instance of a class defined you can call that class's methods.
To create and compile your own classes, you can put the definitions of the classes in the same file where they are being used, or you can put them in their own .java files. Their location will depend on if they are in packages or not.
You should not put any of your code in the folders with the JDK API files.
Re: Java METHOD to return date instead of Calendar field
Can't you just use System.currentTimeMillis and simply substract 1000*60*60*24 from it? then create a calendar instance and retrieve the Date:
Code:
private static final long ONE_DAY_IN_MILLIS = 1000*60*60*24;
long timeInMillis = System.currentTimeMillis();
long yesterdayInMillis = timeInMillis - ONE_DAY_IN_MILLIS ;
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(yesterdayInMillis);
java.util.Date date = cal.getTime();
You could put this in a different class dedicated to handling Dates, DateUtils maybe?
Re: Java METHOD to return date instead of Calendar field
Quote:
Originally Posted by
Norm
To create and compile your own classes, you can put the definitions of the classes in the same file where they are being used, or you can put them in their own .java files. Their location will depend on if they are in packages or not.
But its a java applet that starts from a link on a website in the browser. Does java or browsers like IE and Firefox have directories where java applet files are stored while being run? You're saying to put the custom classes in the directory where the applet is run or "in their own .java files" but I don't know where that is, that's what I'm trying to say. That's why I asked about where the classpath was.
Re: Java METHOD to return date instead of Calendar field
Quote:
Does java or browsers like IE and Firefox have directories where java applet files are stored while being run?
I don't know where the browsers put the files they are working with after they have read them.
Quote:
You're saying to put the custom classes in the directory where the applet is run
All the classes the applet needs to run should be where the browser can find them.
One way is to put all the files in a jar file and use the archive= attribute.
The browser will look for the files the applet needs in the location where it read the html file.
Re: Java METHOD to return date instead of Calendar field
Quote:
Originally Posted by
blaurent
Can't you just use System.currentTimeMillis and simply substract 1000*60*60*24 from it? then create a calendar instance and retrieve the Date:
Code:
private static final long ONE_DAY_IN_MILLIS = 1000*60*60*24;
long timeInMillis = System.currentTimeMillis();
long yesterdayInMillis = timeInMillis - ONE_DAY_IN_MILLIS ;
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(yesterdayInMillis);
java.util.Date date = cal.getTime();
You could put this in a different class dedicated to handling Dates, DateUtils maybe?
This actually looks promising. They're all methods so I think this might work, I'm gonna give it a try.
Re: Java METHOD to return date instead of Calendar field
Quote:
This actually looks promising. They're all methods so I think this might work, I'm gonna give it a try.
Can't say I'd recommend doing this.
Using ONE_DAY_IN_MILLIS = 1000*60*60*24 is fine for all those days that are 24 hours long but a lot of countries use daylight saving and so each year you get one day at 23 hours and one day at 25 hours.