CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Jul 2011
    Posts
    13

    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.

  2. #2
    Join Date
    Jul 2011
    Posts
    13

    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.

  3. #3
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    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.

  5. #5
    Join Date
    Jul 2011
    Posts
    13

    Re: Java METHOD to return date instead of Calendar field

    Quote Originally Posted by Norm View Post
    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?

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  7. #7
    Join Date
    Jul 2011
    Posts
    13

    Re: Java METHOD to return date instead of Calendar field

    Quote Originally Posted by Malisk View Post
    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.

  8. #8
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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.

    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.
    Norm

  9. #9
    Join Date
    Jul 2011
    Posts
    13

    Re: Java METHOD to return date instead of Calendar field

    Quote Originally Posted by Norm View Post
    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 View Post
    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.

  10. #10
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  11. #11
    Join Date
    Jun 2011
    Posts
    35

    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

  12. #12
    Join Date
    Jul 2011
    Posts
    13

    Re: Java METHOD to return date instead of Calendar field

    Quote Originally Posted by Norm View Post
    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.

  13. #13
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  14. #14
    Join Date
    Jul 2011
    Posts
    13

    Re: Java METHOD to return date instead of Calendar field

    Quote Originally Posted by blaurent View Post
    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.

  15. #15
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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 code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured