0

I am aware of the format using (yyyy-MM-dd) but the inputted date must be in yyyy/MM/dd and i am having trouble formatting it cause java wont read the format yyyy/MM/dd how to do this cause in model its in date format and i cannot set it when im returning string so return value must also be in date??

My request parameter from JSP

String HireDate = request.getParameter("hireDate");
    emp.setDoh(DateUtil.StringToDate(HireDate));

MY JQuery

$( function() {
$( "#hireDate" ).datepicker({ dateFormat: "yy/mm/dd" }).val();
});

My Model

private Date doh;
public Date getDoh() {
  return doh;
}

public void setDoh(Date doh) {
  this.doh = doh;
}

DateUtil Class

public static Date StringToDate(String date) {
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", Locale.ENGLISH);
  java.util.Date date1 = null;
  try {
    date1 = sdf.parse(date);
  } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  Date sqlDate = new Date(date1.getTime());
  return sqlDate;
  }

  public static String DateToString(java.util.Date date) {
  String convertedDate = "";
  if(date != null){
  Format formatter = new SimpleDateFormat("yyyy/MM/dd");
  convertedDate = formatter.format(date);
  }
  return convertedDate;
  }
}

I would like to make a static boolean function in another class which i will call to know if its a valid date whenever im using format yyyy/MM/dd in parsing it always returns the whole date including CST year etc,any suggestions??

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Allow me to suggest that in your model you use `LocalDate` from `java.time` rather than a string for the date of hire. – Ole V.V. Jun 20 '18 at 07:55
  • well,,there is valid reason behind that,cause i'm using that same model whenever i'm getting hire_date in oracle database and it does not read other format so i'm using date format in model..Its just that i would want to use it again in jsp instead of making a new model same date but string attribute –  Jun 20 '18 at 07:57
  • 1
    And since you mention `SimpleDateFormat` in your title: avoid that class. It is not only long outdated, it is also notoriously troublesome. The `Date` class is long outdated too. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jun 20 '18 at 07:57
  • wait,,i posted wrong model,so sorry –  Jun 20 '18 at 07:57
  • there was another model of hire date from and hire date to which im using for searching,this is the model for the date im having trouble now,sorry –  Jun 20 '18 at 07:59
  • You should also prefer to pass a `LocalDate` rather than a string to your database.If using JDBC 4.2 or higher, use `PrepareStatement.setObject` and pass a `LocalDate` as the object to set. Your (modern) JPA implementation too will be happy to store a `LocalDate`. – Ole V.V. Jun 20 '18 at 07:59
  • in my code,i can already add data in my database im using to_date('Date String' , 'yyyy/MM/dd') its just that i need a validation kind of method to identify if its in valid format –  Jun 20 '18 at 08:02
  • and the reason why its date there cause i cant get the date in my database if the model is not in date format,in parameter and in data type,,so i really need help right now,is there a way for a date type be checked if its valid by not converting it to string?? –  Jun 20 '18 at 08:03
  • I didn’t get that. No matter if a `Date` or a `LocalDate` it is always valid. You may perform a range check and discard dates that are unreasonably far in the past or the future, of course. – Ole V.V. Jun 20 '18 at 08:05
  • specifically i would like to make a boolean checker to check if its valid format like if(dateUtilityClass.DateChecker(emp.getDate)==true){} then the checker would check if the Date is in yyyy/MM/dd format,,other than that it returns false –  Jun 20 '18 at 08:07
  • Neither a `Date` nor a `LocalDate` has a format. Formats only exist in strings. – Ole V.V. Jun 20 '18 at 08:10
  • i already have a string converter which converts it to string if i input 2018/06/07 in jsp i can make it 2018/06/07 again in string,im having trouble if its not in yyyy/MM/dd format in input,how can i handle it?? –  Jun 20 '18 at 08:11
  • by the way i am really grateful for you having to give some time to a novice like me,greatly appreciated,, ultimately how can i return false or something if its not in that format in string,, –  Jun 20 '18 at 08:11
  • this is my converter of date to stringpublic static String DateToString(java.util.Date date) { String convertedDate = ""; if(date != null){ Format formatter = new SimpleDateFormat("yyyy/MM/dd"); convertedDate = formatter.format(date); } return convertedDate; } –  Jun 20 '18 at 08:16
  • 2
    Welcome to Stack Overflow. Supplementary information for your question is very welcome. Please post it in the question using [the edit link](https://stackoverflow.com/posts/50942981/edit), not in comments. It’s still more important when posting code since unformatted code is unreadable. – Ole V.V. Jun 20 '18 at 08:19
  • Wonder if this is helpful for you? [Link: Date validation in JSP](https://www.roseindia.net/mysql/datevalidation.shtml). Search for more. – Ole V.V. Jun 20 '18 at 08:23
  • the thing is the instruction was must be handle it in backend and all requested attributes must first be validated before calling the class of querying in database if errorList > 0 it will be returned as request attribute and must be displayed,so,,,no –  Jun 20 '18 at 08:26

0 Answers0