Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Java Date

Posted on 2011-05-08
39
Medium Priority
?
578 Views
Last Modified: 2012-05-11

Hello there,

i am trying to insert some dates in my db. i try this,but i get error cannot find symbol.

u.setLastAccessTime(Date.valueOf("1999-7-26"));

cheers
zolf
0
Comment
Question by:zolf
  • 20
  • 14
  • 3
  • +1
39 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35718012
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date dd = f.parse(("1999-7-26", new ParsePosition(0));
u.setLastAccessTime(dd);
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35718019
Assumed that setLastAccessTime() is a method which accepts
java.uti.Date

If you give some details about database that would be useful
0
 

Author Comment

by:zolf
ID: 35718026

thanks a lot.
by the way i am using a block of code from a book. in the book it shows the code like this. i have never seen java code written in parentesis.like i have shown in the code below.is this new feature of java or something.am i missing something
public static void main(String args[]) {
HibernateUtil.recreateDatabase();
Session session = HibernateUtil.beginTransaction();
{
User u = new User();
u.setLoginName("mj");
u.setPassword("abc123");
u.setEmailAddress("mj@mcnz.com");
u.setVerified(false);
u.setLastAccessTime(new java.util.Date());
u.setRegistrationDate(new GregorianCalendar(2006,01,01));
session.saveOrUpdate(u);
}{
User u = new User();
u.setLoginName("mario");
u.setPassword("pass");
u.setEmailAddress("mario@scja.ca");
u.setVerified(true);
u.setLastAccessTime(Date.valueOf("2008-1-1"));
u.setRegistrationDate(new GregorianCalendar(2007,01,01));
session.saveOrUpdate(u);
}{
User u = new User();
u.setLoginName("sk8trgrl");
u.setPassword("password");
u.setEmailAddress("avril@scja.com");
u.setVerified(false);
u.setLastAccessTime(new java.util.Date());
u.setRegistrationDate(new GregorianCalendar(2008,01,01));
session.saveOrUpdate(u);
}
{
User u = new User();
u.setLoginName("ridley");
u.setPassword("mypassword");
u.setEmailAddress("getbent@scja.ca");
u.setVerified(true);
u.setLastAccessTime(new java.util.Date());
u.setLastAccessTime(Date.valueOf("2006-10-5"));
u.setRegistrationDate(new GregorianCalendar(2006,5,11));
session.saveOrUpdate(u);
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 35718028
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35718036
You see Date is ambiguous there are two classes java.uti.Dtae and java.sql.Date
and sql Date does have this method taking this format
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35718048
You see in your code tehy explicuitly wrie java.util.Date
in other cases where Date is without java.util they probably omply java.sql.Date, as they have this sql.Date
in the import on top
0
 

Author Comment

by:zolf
ID: 35718058

 SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
            Date dd = f.parse(("1999-7-26", new ParsePosition(0)));

i get error saying ) expected??i have checked and the brackets seems to be ok
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35718072
try add java.uti.Dtae instead of Date

SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
            java.uti.Date dd = f.parse(("1999-7-26", new ParsePosition(0)));
0
 

Author Comment

by:zolf
ID: 35718073

yes you were correct.java.sql.date solved the issue.can you please throw some light on those bracket thing which i mentioned in the code
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35718079
This code exactly worked for me

  SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
                   Date dd = f.parse("1999-7-26", new ParsePosition(0));
        System.out.println(dd);

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35718082
You had two many parentheses in parsing
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35718091
I'm not sure, waht you mean - there are
soem extra braces there ?
As long as they are matching and the scope of variables are consistent you can put braces, I believe
0
 

Author Comment

by:zolf
ID: 35718096

i mean this

09/05/11 11:34 AM, ID: 35718026
0
 
LVL 92

Expert Comment

by:objects
ID: 35718125
You don't need a DateFormat, the code you already have will work fine
0
 
LVL 92

Expert Comment

by:objects
ID: 35718133
And if you do want to use DateFormat the code you need is shown here
http://helpdesk.objects.com.au/java/how-do-i-parse-a-java-string-that-specifies-a-date
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35718148

Please, explain what you mean by this:
"
i mean this

09/05/11 11:34 AM, ID: 35718026

"
these arer parenthese ()
these are brackets []
these are braces {}
waht do you mean?
0
 

Author Comment

by:zolf
ID: 35718156

i mean braces
0
 

Author Comment

by:zolf
ID: 35718157
0
 

Author Comment

by:zolf
ID: 35718168

this what i am trying to run,but now i get error

Hibernate: insert into Users (emailAddress, lastAccessTime, login_name, password, registrationDate, verified) values (?, ?, ?, ?, ?, ?)
2011-05-09 12:04:12,757 [main] DEBUG IdentifierGeneratorHelper : Natively generated identity: 1
2011-05-09 12:04:12,757 [main] DEBUG AbstractBatcher : about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
Exception in thread "main" java.lang.IllegalArgumentException
        at java.sql.Date.valueOf(Date.java:138)
        at com.exam.main.MainUsers.main(MainUsers.java:72)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
import com.exam.model.Users;
import java.util.GregorianCalendar;
import org.hibernate.Session;

/**
 *
 * @author ZAJ
 */
public class MainUsers
{
    public static void main(String args[])
    {
        
        HibernateUtil.recreateDatabase();
        Session session = HibernateUtil.beginTransaction();
        {
            Users u = new Users();
            u.setLoginName("mj");
            u.setPassword("abc123");
            u.setEmailAddress("mj@mcnz.com");
            u.setVerified(false);
            u.setLastAccessTime(new java.util.Date());
            u.setRegistrationDate(new GregorianCalendar(2006, 01, 01));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("mario");
            u.setPassword("pass");
            u.setEmailAddress("mario@scja.ca");
            u.setVerified(true);
            u.setLastAccessTime(java.sql.Date.valueOf("2008-1-1"));
            u.setRegistrationDate(new GregorianCalendar(2007, 01, 01));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("sk8trgrl");
            u.setPassword("password");
            u.setEmailAddress("avril@scja.com");
            u.setVerified(false);
            u.setLastAccessTime(new java.util.Date());
            u.setRegistrationDate(new GregorianCalendar(2008, 01, 01));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("ridley");
            u.setPassword("mypassword");
            u.setEmailAddress("getbent@scja.ca");
            u.setVerified(true);
            u.setLastAccessTime(new java.util.Date());
            u.setLastAccessTime(java.sql.Date.valueOf("2006-10-5"));
            u.setRegistrationDate(new GregorianCalendar(2006, 5, 11));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("kerri");
            u.setPassword("pwd");
            u.setEmailAddress("sheehan@princessjava.com");
            u.setVerified(false);
            u.setLastAccessTime(java.sql.Date.valueOf("2008-2-25"));
            u.setRegistrationDate(new GregorianCalendar(2007, 12, 12));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("astra");
            u.setPassword("pwd");
            u.setEmailAddress("rabbit@princessjava.com");
            u.setVerified(false);
            u.setLastAccessTime(new java.util.Date());
            u.setRegistrationDate(new GregorianCalendar());
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("cameron");
            u.setPassword("90210");
            u.setEmailAddress("me@scwcd.com");
            u.setVerified(true);
            u.setLastAccessTime(java.sql.Date.valueOf("2008-9-15"));
            u.setRegistrationDate(new GregorianCalendar(2008, 8, 12));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("stephen");
            u.setPassword("low");
            u.setEmailAddress("stanley@pulpjava.com");
            u.setVerified(false);
            u.setLastAccessTime(java.sql.Date.valueOf("2008-2-25"));
            u.setRegistrationDate(new GregorianCalendar(2008, 02, 15));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("che");
            u.setPassword("password");
            u.setEmailAddress("ernesto@pulpjava.com");
            u.setVerified(true);
            u.setLastAccessTime(java.sql.Date.valueOf("1999-7-26"));
            u.setRegistrationDate(new GregorianCalendar(1999, 3, 9));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("remy");
            u.setPassword("password");
            u.setEmailAddress("rabbit@scja.com");
            u.setVerified(false);
            u.setLastAccessTime(new java.util.Date());
            u.setRegistrationDate(new GregorianCalendar(2007, 05, 21));
            session.saveOrUpdate(u);
        }
        HibernateUtil.commitTransaction();

    }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35718177
But they can use extra brcase if all variabes are in csope and opebning match  closing
it probably add readability in this case
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35718186

java.sql.Date.valueOf(Date.java:138)  should have String not date inside the parentheseis
http://download.oracle.com/javase/1.4.2/docs/api/java/sql/Date.html#valueOf%28java.lang.String%29
0
 

Author Comment

by:zolf
ID: 35718191

so is the code which the author has provided got error in it,since i am running his code
0
 

Author Comment

by:zolf
ID: 35718197

i have added string,but still i get error
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35718208
Look you have nothin inline 73
0
 

Author Comment

by:zolf
ID: 35718211

i changed it after that error,but still i get error.here is the new version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.exam.main;

import com.exam.model.Users;
import java.util.GregorianCalendar;
import org.hibernate.Session;

/**
 *
 * @author ZAJ
 */
public class MainUsers
{
    public static void main(String args[])
    {
        
        HibernateUtil.recreateDatabase();
        Session session = HibernateUtil.beginTransaction();
        {
            Users u = new Users();
            u.setLoginName("mj");
            u.setPassword("abc123");
            u.setEmailAddress("mj@mcnz.com");
            u.setVerified(false);
            u.setLastAccessTime(java.sql.Date.valueOf("2008-2-25"));
            u.setRegistrationDate(new GregorianCalendar(2006, 01, 01));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("mario");
            u.setPassword("pass");
            u.setEmailAddress("mario@scja.ca");
            u.setVerified(true);
            u.setLastAccessTime(java.sql.Date.valueOf("2008-1-1"));
            u.setRegistrationDate(new GregorianCalendar(2007, 01, 01));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("sk8trgrl");
            u.setPassword("password");
            u.setEmailAddress("avril@scja.com");
            u.setVerified(false);
            u.setLastAccessTime(java.sql.Date.valueOf("2008-2-25"));
            u.setRegistrationDate(new GregorianCalendar(2008, 01, 01));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("ridley");
            u.setPassword("mypassword");
            u.setEmailAddress("getbent@scja.ca");
            u.setVerified(true);
            //u.setLastAccessTime(new java.util.Date());
            u.setLastAccessTime(java.sql.Date.valueOf("2006-10-5"));
            u.setRegistrationDate(new GregorianCalendar(2006, 5, 11));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("kerri");
            u.setPassword("pwd");
            u.setEmailAddress("sheehan@princessjava.com");
            u.setVerified(false);
            u.setLastAccessTime(java.sql.Date.valueOf("2008-2-25"));
            u.setRegistrationDate(new GregorianCalendar(2007, 12, 12));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("astra");
            u.setPassword("pwd");
            u.setEmailAddress("rabbit@princessjava.com");
            u.setVerified(false);
            u.setLastAccessTime(java.sql.Date.valueOf("2008-2-25"));
            u.setRegistrationDate(new GregorianCalendar());
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("cameron");
            u.setPassword("90210");
            u.setEmailAddress("me@scwcd.com");
            u.setVerified(true);
            u.setLastAccessTime(java.sql.Date.valueOf("2008-9-15"));
            u.setRegistrationDate(new GregorianCalendar(2008, 8, 12));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("stephen");
            u.setPassword("low");
            u.setEmailAddress("stanley@pulpjava.com");
            u.setVerified(false);
            u.setLastAccessTime(java.sql.Date.valueOf("2008-2-25"));
            u.setRegistrationDate(new GregorianCalendar(2008, 02, 15));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("che");
            u.setPassword("password");
            u.setEmailAddress("ernesto@pulpjava.com");
            u.setVerified(true);
            u.setLastAccessTime(java.sql.Date.valueOf("1999-7-26"));
            u.setRegistrationDate(new GregorianCalendar(1999, 3, 9));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("remy");
            u.setPassword("password");
            u.setEmailAddress("rabbit@scja.com");
            u.setVerified(false);
            u.setLastAccessTime(java.sql.Date.valueOf("2008-2-25"));
            u.setRegistrationDate(new GregorianCalendar(2007, 05, 21));
            session.saveOrUpdate(u);
        }
        HibernateUtil.commitTransaction();

    }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35718212
line 73:  u.setLastAccessTime(new java.util.Date());

compare with line 83:

u.setLastAccessTime(java.sql.Date.valueOf("2008-9-15"));

and other lines like that
you line 73 iis wromng
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35718215
Where is new error at what line?
0
 

Author Comment

by:zolf
ID: 35718217

and here is the erro i get

Exception in thread "main" java.lang.IllegalArgumentException
        at java.sql.Date.valueOf(Date.java:138)
        at com.exam.main.MainUsers.main(MainUsers.java:28)
Java Result: 1
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35718235
Don't see, are you sure you pasted the latest version which you are running
73 was inded bad - this 28 maybe I;m not noticing some additional point or comma
try to run once again and post exactly your code

In the worst case you may try to change the order of teese pargraphs
abnd check the line of error - thisometimes helps to spot the misprint
0
 

Author Comment

by:zolf
ID: 35718249

i have changed the line no and ran the file,but still no luck.

Exception in thread "main" java.lang.IllegalArgumentException
        at java.sql.Date.valueOf(Date.java:138)
        at com.exam.main.MainUsers.main(MainUsers.java:30)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.exam.main;

import com.exam.model.Users;
import java.util.GregorianCalendar;
import org.hibernate.Session;

/**
 *
 * @author ZAJ
 */
public class MainUsers
{
    public static void main(String args[])
    {
        
        HibernateUtil.recreateDatabase();
        Session session = HibernateUtil.beginTransaction();
        {
            Users u = new Users();
            u.setLoginName("mj");
            u.setPassword("abc123");
            u.setEmailAddress("mj@mcnz.com");
            u.setVerified(false);

            
            u.setLastAccessTime(java.sql.Date.valueOf("2008-2-25"));
            u.setRegistrationDate(new GregorianCalendar(2006, 01, 01));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("mario");
            u.setPassword("pass");
            u.setEmailAddress("mario@scja.ca");
            u.setVerified(true);
            u.setLastAccessTime(java.sql.Date.valueOf("2008-1-1"));
            u.setRegistrationDate(new GregorianCalendar(2007, 01, 01));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("sk8trgrl");
            u.setPassword("password");
            u.setEmailAddress("avril@scja.com");
            u.setVerified(false);
            u.setLastAccessTime(java.sql.Date.valueOf("2008-2-25"));
            u.setRegistrationDate(new GregorianCalendar(2008, 01, 01));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("ridley");
            u.setPassword("mypassword");
            u.setEmailAddress("getbent@scja.ca");
            u.setVerified(true);
            //u.setLastAccessTime(new java.util.Date());
            u.setLastAccessTime(java.sql.Date.valueOf("2006-10-5"));
            u.setRegistrationDate(new GregorianCalendar(2006, 5, 11));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("kerri");
            u.setPassword("pwd");
            u.setEmailAddress("sheehan@princessjava.com");
            u.setVerified(false);
            u.setLastAccessTime(java.sql.Date.valueOf("2008-2-25"));
            u.setRegistrationDate(new GregorianCalendar(2007, 12, 12));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("astra");
            u.setPassword("pwd");
            u.setEmailAddress("rabbit@princessjava.com");
            u.setVerified(false);
            u.setLastAccessTime(java.sql.Date.valueOf("2008-2-25"));
            u.setRegistrationDate(new GregorianCalendar());
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("cameron");
            u.setPassword("90210");
            u.setEmailAddress("me@scwcd.com");
            u.setVerified(true);
            u.setLastAccessTime(java.sql.Date.valueOf("2008-9-15"));
            u.setRegistrationDate(new GregorianCalendar(2008, 8, 12));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("stephen");
            u.setPassword("low");
            u.setEmailAddress("stanley@pulpjava.com");
            u.setVerified(false);
            u.setLastAccessTime(java.sql.Date.valueOf("2008-2-25"));
            u.setRegistrationDate(new GregorianCalendar(2008, 02, 15));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("che");
            u.setPassword("password");
            u.setEmailAddress("ernesto@pulpjava.com");
            u.setVerified(true);
            u.setLastAccessTime(java.sql.Date.valueOf("1999-7-26"));
            u.setRegistrationDate(new GregorianCalendar(1999, 3, 9));
            session.saveOrUpdate(u);
        }
        {
            Users u = new Users();
            u.setLoginName("remy");
            u.setPassword("password");
            u.setEmailAddress("rabbit@scja.com");
            u.setVerified(false);
            u.setLastAccessTime(java.sql.Date.valueOf("2008-2-25"));
            u.setRegistrationDate(new GregorianCalendar(2007, 05, 21));
            session.saveOrUpdate(u);
        }
        HibernateUtil.commitTransaction();

    }
}

Open in new window

0
 

Author Comment

by:zolf
ID: 35718261

for_yan:

one thing, this method is of type java.util. so could that be the cause. the books code i am following seems to have error in its code.
@Temporal(TemporalType.TIMESTAMP)
    public Date getLastAccessTime()
    {
        return lastAccessTime;
    }

    public void setLastAccessTime(Date lastAccessTime)
    {
        this.lastAccessTime = lastAccessTime;
    }

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35718263
You know wwhat I change to "2008-01-01" and it worked for me
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35718266
You need 2 digits for mionth
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 35718269
java.sql.Date.valueOf("2008-2-25") - does not work

java.sql.Date.valueOf("2008-02-25")  - works
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35718275
>>u.setLastAccessTime(Date.valueOf("1999-7-26")); should be
u.setLastAccessTime(java.sql.Date.valueOf("1999-07-26"));

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35718277
I checked it and one digit is gicves exactly that exception
0
 

Author Closing Comment

by:zolf
ID: 35718377
cheers
0
 
LVL 92

Expert Comment

by:objects
ID: 35718419
> so is the code which the author has provided got error in it,since i am running his code

not that I can see, why do you think it has an error?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35718587
btw zolf, the missing zero was NOT the reason for your error
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question