Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blackberry Java String to date conversion

I used following code to convert string to date but it is applying timezone of device while conversion.
I don't need this but I want same date/time from that string like

String = "2009-07-31 07:59:17.427"
Date = 2009-07-31 07:59:17.427

Date formatter = new Date(HttpDateParser.parse("2009-07-31 07:59:17.427"));
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String strCustomDateTime = dateFormat.format(formatter);
like image 905
Neo Avatar asked Aug 14 '26 11:08

Neo


1 Answers

You may take in account default timezone offset to date you get after parsing:

public static String StringToDate(String dateToParse) {

    Date formatter = new Date(HttpDateParser.parse(dateToParse));
    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss.SSS");
    int offset = TimeZone.getDefault().getRawOffset();
    formatter.setTime(formatter.getTime() + offset);
    String strCustomDateTime = dateFormat.format(formatter);
    return strCustomDateTime;
}
like image 147
Maksym Gontar Avatar answered Aug 17 '26 02:08

Maksym Gontar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!