-1

When I select date in SQL it is returned as:

Wed Jan 31 00:00:00 GMT+01:00 2018 

But I need only the Date part, that is Jan 31 2018. How can I do this?

Gholamali Irani
  • 4,391
  • 6
  • 28
  • 59
  • Off hand...Parse the string from JXDatePicker: `String[] dateParts = dateString.split("\\s+"); String newDateString = dateParts[1] + " " + dateParts[2] + " " + dateParts[5];`. See also this [SO Post](https://stackoverflow.com/questions/14999506/convert-string-date-to-string-date-different-format) – DevilsHnd - 退職した Jan 12 '18 at 23:50

1 Answers1

0

There may be a reason to use org.jdesktop.swingx.JXDatePicker but rather of using JXDatePicker, I will simply show GMT parsing using java.util.Date.

Try this source code:

System.out.println(new Date());
//will show your Date along with your local TimeZone
//result for me is : Sat Jan 13 08:47:59 IST 2018

//First changing local pacific time zone to GMT+01 level explicitly,
//otherwise it will show results as your local time zone by default.
TimeZone.setDefault(TimeZone.getTimeZone("GMT+01"));

String existingDateValue = "Wed Jan 31 00:00:00 GMT+01:00 2018";
DateFormat gmtFormat = 
        new SimpleDateFormat("E MMM dd hh:mm:ss zzz yyyy");
try {
    //try to parse and validate existing date
    Date validatedExistingDate = gmtFormat.parse(existingDateValue);
    System.out.println(validatedExistingDate);
    //parsed validated date : Wed Jan 31 00:00:00 GMT+01:00 2018

    DateFormat newFormat = new SimpleDateFormat("MMM d, yyyy");
    System.out.println(newFormat.format(validatedExistingDate));
    //required Date is in GMT format : Jan 31, 2018
} catch (ParseException pex) {
    pex.printStackTrace();
}  finally {
    System.out.println("Current TimeZone : " + new Date());
    //now, reverting to my local TimeZone
    TimeZone.setDefault(TimeZone.getTimeZone("IST"));
    System.out.println("Current TimeZone : " + new Date());
}
ArifMustafa
  • 4,617
  • 5
  • 40
  • 48