javadatabasehibernatedatetime

Tried to get current time but got date in 1970


How do I get Current Time as this code is giving (Time-->Thu Jan 01 05:56:27 ACT 1970)??

DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss.SS"); 
         Date time = new Date(); 
         String currentTime=timeFormat.format(time);
         time=timeFormat.parse(currentTime);
         System.out.println("Time-->"+time);
         salesOrder.setOrderTime(time);

I want separate Date and Separate time ? How am I gonna get them? I mean, I am having two separate columns in my database, one for Date and other for Time. My Bean is Mapped using Hibernate with database and I need Object of Date type as in database, I am having (Date type) for Date and (Time type) for Time. How will I separate them to store them in different columns in database?


Solution

  • use DateFormat timeFormat = new SimpleDateFormat(); 
    

    instead of

    DateFormat timeFormat = new SimpleDateFormat("HH:MM:ss:SS");
    

    The problem is in formatting. You have not provided the Day,year fieldr, thats why it is acting that way. Or You can use this with proper formatting :

    DateFormat timeFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
    

    EDIT:

    Try this:

     Calendar c = Calendar.getInstance(); 
      c.setTime(time);
      System.out.println(c.get(Calendar.YEAR)+ "  "+c.get(Calendar.MONTH)+ " "+ c.get(Calendar.DAY_OF_MONTH));
    

    Where time set in Calendar object is time that is not parsed/formatted using simpleDateFormat. From calendar object, you can get individual month , day , year and use it the way you like, or you can just call c.getTime() to get the Date object.