0

I am getting the date in the format Mon Nov 09 2015 00:00:00 GMT+0530 (India Standard Time). I have to convert it to sql date of format dd/MM/yyyy. Please help.

I tried below code but did not help

Date date = new java.sql.Date(
(new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss zz (zzzz)").parse("Mon Nov 09 2015 00:00:00 GMT+0530 (India Standard Time)"))
.getTime());
Suraj
  • 2,181
  • 2
  • 17
  • 25
user1374266
  • 323
  • 1
  • 4
  • 19
  • I think that main problem is with part of string `(India Standard Time)`. Try to cut it off – Andremoniy Nov 09 '15 at 07:58
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `DateTimeFormatter` and either `ZonedDateTime` or `OffsetDateTime`; all three are from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). See the answer by Arvind Kumar Avinash. – Ole V.V. Mar 28 '21 at 17:00

3 Answers3

4

Something like below, also refer here for Java SimpleDateFormat

public static void main(String[] args) throws ParseException {
        String time = "Mon Nov 09 2015 00:00:00 GMT+0530 (India Standard Time)";
        DateFormat inputFormat = new SimpleDateFormat(
                "E MMM dd yyyy HH:mm:ss 'GMT'z", Locale.ENGLISH);
        Date date = inputFormat.parse(time);
        System.out.println(date);

        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
        System.out.println(formatter.format(date));
    }

output

Mon Nov 09 00:00:00 IST 2015
09/11/2015
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
1

The java.util date-time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API* .

Since your date-time string has timezone information, parse it into ZonedDateTime which you can convert to other java.time types and if required, format into the desired formats.

Demo:

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.TextStyle;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "Mon Nov 09 2015 00:00:00 GMT+0530 (India Standard Time)";
        
        DateTimeFormatter dtfInput = new DateTimeFormatterBuilder()
                                        .parseCaseInsensitive()
                                        .appendPattern("E MMM d uuuu H:m:s")
                                        .appendLiteral(" ")
                                        .appendZoneId()
                                        .appendPattern("X")
                                        .appendLiteral(" ")
                                        .appendLiteral("(")
                                        .appendGenericZoneText(TextStyle.FULL)
                                        .appendLiteral(')')
                                        .toFormatter(Locale.ENGLISH);
        
        ZonedDateTime zdt = ZonedDateTime.parse(strDateTime, dtfInput);
        OffsetDateTime odt = zdt.toOffsetDateTime();
        System.out.println(odt);
        
        // To LocalDate, LocalDateTime etc.
        LocalDate localDate = odt.toLocalDate();
        LocalDateTime localDateTime = odt.toLocalDateTime();
        System.out.println(localDate);
        System.out.println(localDateTime);
        
        // Get the formatted string
        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("dd/MM/uuuu", Locale.ENGLISH);
        String formatted = dtfOutput.format(localDate);
        System.out.println(formatted);
        
        // In UTC
        odt = odt.withOffsetSameInstant(ZoneOffset.UTC);
        System.out.println(odt);
    }
}

Output:

2015-11-09T00:00+05:30
2015-11-09
2015-11-09T00:00
09/11/2015
2015-11-08T18:30Z

Learn more about the modern date-time API from Trail: Date Time.

Support for OffsetDateTime in JDBC:

Starting with JDBC 4.2, you can use OffsetDateTime directly in your code e.g.

PreparedStatement st = conn.prepareStatement("INSERT INTO mytable (columnfoo) VALUES (?)");
st.setObject(1, odt);
st.executeUpdate();
st.close();

The support for java.time API in JDBC Java SE 8 (JDBC 4.2) came without requiring any public JDBC API changes. The setObject and getObject methods support java.time types as per the following mapping:

enter image description here

Note for PostgreSQL™: ZonedDateTime, Instant and OffsetTime / TIME WITH TIME ZONE are not supported. Also, the OffsetDateTime instances need to be in UTC (have offset 0).


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Good to have a modern answer to this question (’twas about time). Thank you. I’d probably go with `DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss 'GMT'xx (zzzz)", Locale.ROOT)`. I might also parse both into an `OffsetDateTime` and a `ZonedDateTime` and check that the offsets agree, for validation. – Ole V.V. Mar 29 '21 at 04:57
  • The format, `DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss 'GMT'xx (zzzz)` would be limited to `GMT` but the parser in this answer will work also for `UTC` (e.g. `Mon Nov 09 2015 00:00:00 UTC+0530 (India Standard Time)` without any change. – Arvind Kumar Avinash Mar 29 '21 at 05:34
0

I have dealt with something like this right now, and this is my solution:

 String dateString = new SimpleDateFormat("dd/MM/yyyy").format(timestamp.toDate()).toString();