|
-
July 5th, 2011, 04:03 PM
#1
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.
-
July 5th, 2011, 04:51 PM
#2
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.
-
July 5th, 2011, 09:04 PM
#3
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);
Norm
-
July 6th, 2011, 03:30 AM
#4
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.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
July 6th, 2011, 08:21 AM
#5
Re: Java METHOD to return date instead of Calendar field
 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?
-
July 6th, 2011, 08:27 AM
#6
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?
Norm
-
July 6th, 2011, 11:26 AM
#7
Re: Java METHOD to return date instead of Calendar field
 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?
Last edited by Malisk; July 6th, 2011 at 11:31 AM.
-
July 6th, 2011, 11:32 AM
#8
Re: Java METHOD to return date instead of Calendar field
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.
Does this have anything to do with the import statement?
Or does your program have a classloader that loads classes or uses reflection.
Norm
-
July 6th, 2011, 01:14 PM
#9
Re: Java METHOD to return date instead of Calendar field
 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?
 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.
Last edited by Malisk; July 6th, 2011 at 01:21 PM.
-
July 6th, 2011, 01:58 PM
#10
Re: Java METHOD to return date instead of Calendar field
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.
Norm
-
July 6th, 2011, 02:04 PM
#11
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?
Last edited by blaurent; July 6th, 2011 at 02:05 PM.
Reason: typo
-
July 25th, 2011, 10:22 AM
#12
Re: Java METHOD to return date instead of Calendar field
 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.
-
July 25th, 2011, 10:31 AM
#13
Re: Java METHOD to return date instead of Calendar field
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.
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.
Norm
-
July 25th, 2011, 10:48 AM
#14
Re: Java METHOD to return date instead of Calendar field
 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.
-
July 25th, 2011, 11:48 AM
#15
Re: Java METHOD to return date instead of Calendar field
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|