ankita
ankita

Reputation: 11

Convert the date time in yyyy-MM-dd HH:mm:ss" format

Hi I am new to java and JPQL. here I am passing the dateTime as parameter in my url of GET method. what I want is I want user should pass date time in "yyyy-MM-dd HH:mm:ss" format.But right now when I pass the date time it is comapring with the current dateTime .I want it should compare the reservations by the date and time passed by user. here is my method and query. please help me..

@GET
    @Path("/getReservationByUserIdAndTypeAndDateTime/{userid}/{type}/{dateTime}")
    public List<Reservation> getReservationByAndUserIdAndTypeAndDateTime(@PathParam("userid") int uid, @PathParam("type") int tid,@PathParam("dateTime")String dateTime) {
        CriteriaBuilder builder = em.getCriteriaBuilder();

        //Timestamp dateTime = new Timestamp(System.currentTimeMillis());
     dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());

        //System.out.println("dateTime-"+dateTime);

        CriteriaQuery<Reservation> criteria = builder.createQuery(Reservation.class);
        Root<Reservation> r = criteria.from(Reservation.class);
        TypedQuery<Reservation> query = em.createQuery(
            criteria.select(r).where(new Predicate[]{
                    builder.equal(r.get(Reservation_.usersId), uid), 
                    builder.equal(r.get(Reservation_.reservationsType), tid),
                    builder.greaterThanOrEqualTo(
                           builder.concat(r.get(Reservation_.date), r.get(Reservation_.time)), dateTime.toString())}));
        return query.getResultList();
    }

Upvotes: 0

Views: 7891

Answers (2)

waseemwk
waseemwk

Reputation: 1509

It is Very Simple just use this method

String myDateFormat = "yyyy-MM-dd HH:mm:ss";

public String formatDate(Date date)
{
    SimpleDateFormat customFormat = new SimpleDateFormat(myDateFormat );
    customFormat.setLenient(false);
    return customFormat.format(date);
}

you Can convert your Date in String into Date Object by new Date('your date in String')

Upvotes: 3

Farooq Arshed
Farooq Arshed

Reputation: 1963

Try

dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Integer.parseInt(dateTime));

instead of

dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());

Your datetime which the user is passing should be in unix time stamp.

Upvotes: -1

Related Questions