0

Using HTML for client side and JAVA for server side, Im trying to convert two text inputs into a Date object in java. my first input should hold the date in format DD/MM/YYYY and my second should have time HH:MM formatted.

What ive tried was using input type="text" for both, but im unable to form the object properly on the server's side.

Could you please provide me a simple example for doing that properly? Please ignore any validations and of course and you can assume my main problem is the parsing itself.

5
  • 2
    Is this a Java question? Or did you want javascript? Commented Jan 9, 2014 at 14:24
  • Ive updated, Java on the server's side. Commented Jan 9, 2014 at 14:26
  • Parse them via the SimpleDateFormat Commented Jan 9, 2014 at 14:26
  • how do you that while combining them both and keeping only single Date object? Commented Jan 9, 2014 at 14:28
  • Grab both of them, concatenate the data into a string or something, then parse them according to a certain format which you specified beforehand which matches the pattern of the string you created from both of the values. There's a reference to the patterns: here Check the Example section there for actual examples containing both date and time. Commented Jan 9, 2014 at 14:29

2 Answers 2

2

You can do it with a DateFormat (even a SimpleDateFormat) like so

private static String DATE_PART = "dd/MM/yyyy";
private static String HOUR_PART = "HH:mm";
private static java.text.DateFormat FORMAT = 
    new java.text.SimpleDateFormat(DATE_PART + " " + HOUR_PART);
private static java.text.DateFormat HOUR_FORMAT = 
    new java.text.SimpleDateFormat(HOUR_PART);
private static java.text.DateFormat DATE_FORMAT = 
    new java.text.SimpleDateFormat(DATE_PART);

public static Date fromStrings(String date, String time) {
    StringBuilder sb = new StringBuilder(date);
    sb.append(" ").append(time);
    try {
        return FORMAT.parse(sb.toString());
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}
public static void main(String[] args) {
    String date ="09/01/2014";
    String time ="16:05";
    java.util.Date dt = fromStrings(date, time);
    System.out.println(dt);
    System.out.println(DATE_FORMAT.format(dt));
    System.out.println(HOUR_FORMAT.format(dt));
}

Which outputs

Thu Jan 09 09:28:00 EST 2014
09/01/2014
16:05
Sign up to request clarification or add additional context in comments.

6 Comments

hmm, there's a slight bug, i tried DD/MM/YYYY and HH:MM instead of your pattern and while using the fromString im getting something bit different then what ive expected
Read the format again (it's documented at that link).. "MM" is month in year, "mm" is minutes of day (and capitalization is significant).
So it might be not related to your solution but to my understanding. I used "09/01/2014" and "16:05" and recieved a corrected "2014-01-09T16:05:00+02:00" object. now, while trying to formating it i used: new SimpleDateFormat("dd/MM/yyyy HH:mm").format(date); but i recieved as a result: 09/01/2014 16:01, Ain't sure about why the 01 appears at the end.
It works for me - String date ="09/01/2014"; and String time ="16:05"; and java.util.Date dt = fromStrings(date, time); outputs Thu Jan 09 16:05:00 EST 2014.
Your logic is correct but after im getting a Date object I'm trying to string it back to a separated form using SimpleDateFormat. Am I missing something?
|
1
String date = "dd/MM/yyyy"; // your date value from user input
String hour = "HH:mm"; // your hour value from user input    

try {
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");

    String tempDate = date + " " + hour;
    Date result = dateFormat.parse(tempDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }

Extract it into a method if you wish.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.