Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SimpleDateFormat

Status
Not open for further replies.

scripter73

Programmer
Apr 18, 2001
421
US
Hi,

Does anyone know the simplest way to take a current date like now, and convert into a MM.DD.YY format?

I went out on the net and I've seen several Java docs for SimpleDateFormat, but not one good example.

I'd like to do it in a simple <%= ..... %> if possible.

Also, would I need to import a package like java.text.SimpleDateFormat?

This can't be as difficult as these sites profess.

Thanks in advance for your help.

scripter73



Change Your Thinking, Change Your Life.
 
All,

I tried this:


<%@ page import="java.text.SimpleDateFormat" %>

<%

SimpleDateFormat todaysdate = new SimpleDateFormat("dd.MM.yy");

%>

....

Tried to use it here:

<%= todaysdate %>



I received a printout, but its not what I want:

Today's Date: java.text.SimpleDateFormat@8f0f8ae0

Why do I get this?

Thanks in advance.

scripter73


Change Your Thinking, Change Your Life.
 

Hi,

This worked great. Nice to see there's a quick way.

Thanks for your help.

scripter73


Change Your Thinking, Change Your Life.
 
Hi,

Another question regarding date. How can I say,

Take a date in MM/DD/YYYY - # Days?

Example:

Today's Date - 30 days?

Thanks in advance.

scripter73


Change Your Thinking, Change Your Life.
 
Because its Friday, here's the code for one way of doing it ...

Code:
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		java.util.Date master = sdf.parse("01/03/2004", new ParsePosition(0));
		long masterLong = master.getTime() ;
		long oneDay = (1000 * 60 *60 *24);
		long thirtyDays = (oneDay * 30);
		java.util.Date minusThrityDays = new java.util.Date(masterLong - thirtyDays);
		System.err.println(master);
		System.err.println(minusThrityDays);
 
Thanks a bunch, sedj!

I rewrote (system.err.println) to (out.println) so I can see the results.

However, I'm getting a compilation error.

2004-04-30 11:51:39 StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 11 in the jsp file: /searchResults.jsp

Generated servlet error:
[javac] Compiling 1 source file

C:\Tomcat 5.0\work\Catalina\localhost\pos-webapp\org\apache\jsp\searchResults_jsp.java:196: cannot resolve symbol
symbol : class ParsePosition
location: class org.apache.jsp.searchResults_jsp
java.util.Date master = sdf.parse("01/03/2004", new ParsePosition(0));
^
1 error


at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:127)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:351)



Change Your Thinking, Change Your Life.
 
Hi sedj,

NEVERMIND! Dummy error. I had to add java.text.*.

I already had it, but it was java.text.SimpleDateFormat.

Let me take a look at this and I'll let you know how I formatted it.

Thanks for the help.

scripter73


Change Your Thinking, Change Your Life.
 
scripter73 :

Learn to help yourself ...

First identify the error :

C:\Tomcat 5.0\work\Catalina\localhost\pos-webapp\org\apache\jsp\searchResults_jsp.java:196: cannot resolve symbol
symbol : class ParsePosition


means that the compiler does not know what "ParsePostion" is. So check it out on the web ...

If you google for "ParsePostion Java" you will get this link as the top hit :


So this tells you that ParsePosition is actually java.text.ParsePosition so you need to add java.text.* or java.text.ParsePosition in your imports ...
 
Hi sedj,

Me....again.

I looked at your routine and that's really cool the way you can parse the string. I'm confused, though on the format I'm getting back of "Mon Mar 01 00:00:00 EST 2004 ".

Here's what I'm trying to do.

I want to be able to manipulate Today's Date in the following ways: yesterday, today's date - 7 days (week), today's date - 30 days, today's date - 60 days,

How could I do that with respect to your function and return the format as MM/DD/YYYY?

I would try to modify it, but I'm not sure what you're doin with masterLong, oneDay, and thirtyDays.

Any help you can provide is GREATLY appreciated. I've been trying to work this out for a while. This is the furthest I've gotten.

Thanks.




Change Your Thinking, Change Your Life.
 
Hi sedj,

I reviewed it again and I think I know what you're doing. The timestamp was throwing me off a bit.

I was successful in getting the various dates I needed.

Now, I only have to convert to mm/dd/yyyy format.

Thanks again.




Change Your Thinking, Change Your Life.
 
OK, some basics ...

The class Object has a "toString()" method. If the Object that you have instantiated (remember that all Java classes inherit Object) is requested to call the Object.toString() method without overidding it, you will see something like "bla.bla.myboject@sfsdf334f" if you do a System.err.println() or System.out.println() on it. This is because when you invoke these method (or any similar IO print method) the Object method toString() is called.

Now if an Object such as java.util.Date is passed to Ssytem.err.println(), and hence invokes the toString() method, then this object overrides (it doesn;t have to, but this one does) , and you see some outout such as you would see from
System.err.println(new java.util.Date());

ie a timestamp.

You can test this ....

Code:
public class Test {

 class PrintCustom {
   public String toString() {
     return "My custom String";
   }
 }

 class UseObject {

 }

 public static void main(String args[]) {
   System.err.println(new PrintCustom());
   System.err.println(new UseObject());
 }
}

See what it is doing ?


Now, if you need your "minus thiry days date object" to be in mm/dd/yyyy" format, you need to pass it into a SimpleDateFormat again ...
 
Thanks, sedj,

I actually reviewed it again and saw what you were talking about with ParsePosition, etc.




Change Your Thinking, Change Your Life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top