1

I have inserted date column in MySQL using hibernate JPA it is inserted with correct date and time but when i am retrieving the column in my java code it is giving me time six hours late

this is my model a.java:

import java.util.Date;
@CreationTimestamp
@Column(name = "Created_at", updatable = false, nullable = true)
private Date Created_at;
public Date getCreated_at() {
        return Created_at;
    }
public void setCreated_at(Date created_at) {
        Created_at = created_at;
    }

this is my repository code:

@Repository
@Transactional
public interface abcRepository extends JpaRepository<abc, String> {

@Query(value = "select Created_at from tbl where UserId =:UserId", nativeQuery = true)
    public List<Object[]> getList(@Param("UserId")String walletUserId);
}

this is my main code where i am retrieving data:

List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> mapofStringObject = new LinkedHashMap<String, Object>();
List<Object[]> listDetails = null;
listDetails = abc.getList(UserId);

for (Object[] o : listDetails) {

mapofStringObject = new LinkedHashMap<String, Object>();        
mapofStringObject.put("paymentCreated_at", o[0]);
list.add(mapofStringObject);
}

i expect the date and time like in mysql table like 2019-09-04 10:23:31 but actually it gives me output 2019-09-04T04:53:31.000+0000 i,e six hours late time mysql table image

I am trying this i am getting

code:

TimeZone tz = c.getTimeZone();
Calendar c2 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss z");
System.out.println(sdf.format(c2.getTime()));
System.out.println("Current TimeZone is : " + tz.getDisplayName());

output:

  • 04/09/2019 05:24:33 UTC
  • Current TimeZone is : Coordinated Universal Time

in mysql i have tried:

SELECT @@global.time_zone, @@session.time_zone;

output

mysql timezone

user9862376
  • 29
  • 10
  • What is your timezone? I am betting it is 6 hours before UTC ... – Stephen C Sep 04 '19 at 05:12
  • i have edited the code can you please check, n tell me where i am wrong it would be great help @StephenC – user9862376 Sep 04 '19 at 05:31
  • Is there a difference in the TimeZone between where you are and where the server is? What is the TimeZone of your windows clock? What is the TimeZone of the server windows clock? Or is the program inserting the value being ignorant/considerate of a TimeZone that the server is being considerate/ignorant of? We can't see any of your code that actually generates a date value and plugs it in – Caius Jard Sep 04 '19 at 05:31
  • It would appear that you are storing a UTC time in the database. Which is fine. But my guess is that you are displaying it as if it was local time. This might help: https://stackoverflow.com/questions/12487125/java-how-do-you-convert-a-utc-timestamp-to-local-time – Stephen C Sep 04 '19 at 05:45
  • yes i think this the problem,but i dont know how to check or set it @CaiusJard – user9862376 Sep 04 '19 at 06:33
  • If you're using Windows, you could adjust the time zone according to your location first then perform "mysql_upgrade". I'm not sure what version of MySQL you're using but that's what I did one of our server running MariaDB 10.3. – FanoFN Sep 04 '19 at 08:15
  • Maybe you can change MySql TimeZone. https://stackoverflow.com/questions/930900/how-do-i-set-the-time-zone-of-mysql – Min Hyoung Hong Sep 04 '19 at 13:31

1 Answers1

0

mysql timezone was IST and in it was taking utc format, so i have change timezone to IST in code

How to Parse Date from GMT TimeZone to IST TimeZone and Vice Versa in android

user9862376
  • 29
  • 10