0

In my project new Date(); gives date in other language. And it is giving error, can someone please provide solution to convert darte in english?

my code is as below:

    model.put("total", timeSheetTempDAO.getCurrentDayHours(SessionManagement.getCurrentUserId(),new SimpleDateFormat("YYYY-MMM-dd").format(new Date())));

org.postgresql.util.PSQLException: ERROR: invalid input syntax for type date: "२०१७-जनवरी-२६" Position: 136 org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103) org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836) org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257) org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512) org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388) org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:273) org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:56) org.hibernate.loader.Loader.getResultSet(Loader.java:2040) org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1837) org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1816) org.hibernate.loader.Loader.doQuery(Loader.java:900) org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342) org.hibernate.loader.Loader.doList(Loader.java:2526) org.hibernate.loader.Loader.doList(Loader.java:2512) org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2342) org.hibernate.loader.Loader.list(Loader.java:2337) org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:495) org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:357) org.hibernate.engine.

prem30488
  • 2,828
  • 2
  • 25
  • 57
  • 2
    It looks to me like you need to set an appropriate [Locale](https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html) in the second argument of your `SimpleDateFormat` constructor. – Joe C Jan 26 '17 at 00:04

1 Answers1

1

tl;dr

  • Specify a Locale for your SimpleDateFormat
  • Fix your formatting pattern, yyyy-MM-dd
  • Better to use java.time classes instead of these troublesome legacy classes

Current date in a String:

LocalDate.now( 
    ZoneId.of( "America/Montreal" ) 
).toString()

Simplify your code when debugging

Tear apart your code to debug this kind of problem. You show one long complicated line of code. Once separated, you would see the problem has nothing to do with Postgres nor with JDBC/Hibernate.

Incorrect formatting pattern

You are using the incorrect formatting code for SimpleDateFormat. Uppercase Y means week-year rather than calendar-year. The triple-M means the name of the month, not a number. For SimpleDateFormat, use yyyy-MM-dd (but you should really be using java.time classes).

Pass objects, not strings, through JDBC

Do not pass date-time values as strings through JDBC to your database. Use the date-time types. One purpose of JDBC is to bridge the difference between the Java types and the data types of your database. Search Stack Overflow for "JDBC" and "LocalDate" for more discussion and examples.

Time zone

You are ignoring the issue of time zone.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

The SimpleDateFormat class implicitly applies the JVM’s current default time zone. So your results may vary. Better to always explicitly specify your desired/expected time zone.

Locale

Likewise, you are ignoring the issue of Locale when generating the string representing your date-time value. Locale determines (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, and such. The locale has nothing to do with time zone, by the way.

The SimpleDateFormat class implicitly uses your JVM’s current default Locale. Better to always explicitly specify your desired/expected locale. Perhaps Locale.UK or Locale.ENGLISH.

java.time

You are using troublesome old date-time classes. I suggest you learn about the java.time classes and consider their use, although I do not know about support for java.time in Hibernate.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154