0

I have very simple question - I read couple of threads here but I still do not understand how to get simple thing. I want to send string to method and get back joda date. I had no problem to build it up, but return format is 2015-03-11T17:13:09:000+01:00. How can I get desired (e.g. mmm-dd hh:mm) format back from below mentioned method (it mustto be a dateTime for sorting purposes on FX form)? I tried to gamble with another dateTimeFormatter but had no luck. Thank you very much in advance

public static DateTime stringToDateTime(String textDate) throws ParseException
{
    DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
    DateTime jodaTime = dateTimeFormatter.parseDateTime(textDate);

    return jodaTime;
}
Cavas
  • 37
  • 1
  • 1
  • 4

2 Answers2

0

What do you mean by "return format"? "Format" term here could only be related to a string representation of a DateTime object. That means you should specify format of your input string (what you've already done in your code) - and a corresponding DateTime object will be created. After that you probably use toString() to check the results, but DateTime.toString() uses ISO8601 format (yyyy-MM-ddTHH:mm:ss.SSSZZ) according to JavaDoc - that gives you your 2015-03-11T17:13:09:000+01:00 result.

So to get it as desired you could try using toString(String pattern) method with format you need. But once again - it's just an output format to convert DateTime to String, it doesn't affect the datetime stored in your DateTime object.

FlasH from Ru
  • 1,165
  • 2
  • 13
  • 19
  • HIi FlasH. thx for yourfeedback.I will try to describe a bit more entire setup. I am pulling data into recordset from MS SQL DB, this particular column is in DateTime format. I am looping the recordset, this column as DateFormater.stringToDateTime(rs.getString("Date_Sent") - using the above method. This is then binded in controller of my form designed in scene builder as @FXML private TableColumn emailReceivedColumn and emailReceivedColumn.setCellValueFactory(new PropertyValueFactory("dateReceived")). I cant overide toString, can you expain a bit more your suggestion? thx! – Cavas Apr 16 '15 at 12:40
  • I'm not familiar with javaFX, but it seems to me that you actually _can_ override format for the column - use cell factories. See http://stackoverflow.com/questions/10147325/set-font-in-javafx/10149050#10149050 I.e. from my understanding you should define you own cell factory and implement your own `updateItem(..)` method to print `DateTime` in correct format. – FlasH from Ru Apr 16 '15 at 13:13
  • hi once again Flash. I will take a look on this, I am just starting with Java so things which might seems to be very easy in my eyes, such as change date format, are at the end quite complex things. – Cavas Apr 16 '15 at 13:40
0

I just use Calendar object so this is a possible way to do it:

    static String stringToDateTime(String textDate) {

    Calendar c = new GregorianCalendar();
    // How you want the input to be formatted
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
        Date date = df.parse(textDate);
        c.setTime(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    // How do you want to print your date
    df= new SimpleDateFormat("dd-MM-yy");
    return df.format(c.getTime());
}

// input
    String myDate = "2015-04-15 14:25:25";
    System.out.println(stringToDateTime(myDate));
Black_Buster
  • 102
  • 5