Different time & date from the database and what shown at my web page using Spring MVC

huangapple go评论57阅读模式
英文:

Different time & date from the database and what shown at my web page using Spring MVC

问题

I have some issue about my date & time that I've stored at the database (using SQL) it's different when I show it at my web page. There's different at date & time it self.

For example:

Stored in DB : 2023-05-20 18:12:48 (Right Date & Time)

At Web Page : 2023-05-21 01:12:48.0 (Wrong Date & Time)

I've set the default date & time based on right now time, so when the user clicks the button they will save the date & time by the time when they click at it.

The date & time that stored in the database it's the right one. But when I show it on the web page, it comes out different (wrong date & time on the page).

This is the entities table:

@Entity
@Table(name = "attendance_check_in")

public class AttendanceCheckIn {

    @Column(name = "check_in")
    @Temporal(TemporalType.TIMESTAMP)
    private Date checkIn;

    //getter & setter//
}

I am using SQL database and Spring. Here's my controller that handles the save request to the database:

@RequestMapping(value = "/attendance-input", method = RequestMethod.POST)
public String inputAttendance(Model model, AttendanceCheckIn attendanceIn, Authentication auth) throws ParseException {

    model.addAttribute("attendanceIn", new AttendanceCheckIn());
    LocalDateTime ldt = LocalDateTime.now();
    Date in = Date.from(ldt.toInstant(ZoneOffset.UTC));
    attendanceIn.setCheckIn(in);
    attendanceInServ.save(attendanceIn);

    return "redirect:/attendance";
}

So, basically I've set the default value for date & time as right now time. So when the button clicked it stores the value, and the value that stored in the database is right as what I want. This is the date & time that saved in the database:

But when I see the value that renders on the web page, this is what I get:

I checked my controller that handles the request to show the data from the database:

@RequestMapping(value = "/attendance", method = RequestMethod.GET)
public String attendancePage(Model model) 

    model.addAttribute("attendanceIn", new AttendanceCheckIn());
    //Attendance Check In
    model.addAttribute("getAllAttendanceIn", attInServices.getAll());

    return "Attendance";
}

And, this is from the table on my web page that uses Thymeleaf & Bootstrap data tables:

<table id="attIn-table" 
       class="table table-bordered" 
       width="100%" 
       cellspacing="0">

     <thead>
        <tr>
           <td>Date & Time</td>
        </tr>
     </thead>
     <tbody>
        <tr th:each="as : ${getAllAttendanceIn}">
           <td th:text="${as.checkIn}"></td>
        </tr>
     </tbody>
 </table>

Can someone tell me what happened and how to solve this? Many Thanks

英文:

I have some issue about my date & time that I've stored at the database (using SQL) it's different when I show it at my web page. There's different at date & time it self.

For example :

Stored in DB : 2023-05-20 18:12:48 (Right Date & Time)

At Web Page : 2023-05-21 01:12:48.0 (Wrong Date & Time)

I've set the default date & time based on right now time, so when the user click the button they will save the date & time by the time when they click at it.

The date & time that stored in database it's the right one. But when I show it at the web page, it's comes out different (wrong date & time at the page).

This's the entities table :

@Entity
@Table(name = &quot;attendance_check_in&quot;)

public class AttendanceCheckIn {

     @Column(name = &quot;check_in&quot;)
     @Temporal(TemporalType.TIMESTAMP)
     private Date checkIn;

     //getter &amp; setter//

I'am using SQL database and using Spring. Here's my controller that handle the save request to the database :

@RequestMapping(value = &quot;/attendance-input&quot;, method = RequestMethod.POST)
 public String inputAttendance(Model model, AttendanceCheckIn attendanceIn, Authentication auth) throws ParseException {

      model.addAttribute(&quot;attendanceIn&quot;, new AttendanceCheckIn());
      LocalDateTime ldt = LocalDateTime.now();
      Date in = Date.from(ldt.toInstant(ZoneOffset.UTC));
      attendanceIn.setCheckIn(in);
      attendanceInServ.save(attendanceIn);

      return &quot;redirect:/attendance&quot;;
 }

So, basically I've set the default value for date & time as right now time. So when the button clicked it's store the value, and the value that stored in database is right as what I want. This is the date & time that saved in database :

Different time & date from the database and what shown at my web page using Spring MVC

But when I see the value that render on the web page, this what I get :
Different time & date from the database and what shown at my web page using Spring MVC

I checked my controller that handle request to show the data from the database :

 @RequestMapping(value = &quot;/attendance&quot;, method = RequestMethod.GET)
 public String attendancePage(Model model) 

      model.addAttribute(&quot;attendanceIn&quot;, new AttendanceCheckIn());
      //Attendance Check In
      model.addAttribute(&quot;getAllAttendanceIn&quot;, attInServices.getAll());

      return &quot;Attendance&quot;;
 }

And, this from table at my web page that using thymeleaf & bootstrap data tables :

&lt;table id=&quot;attIn-table&quot; 
       class=&quot;table table-bordered&quot; 
       width=&quot;100%&quot; 
       cellspacing=&quot;0&quot;&gt;

     &lt;thead&gt;
        &lt;tr&gt;
           &lt;td&gt;Date &amp; Time&lt;/td&gt;
        &lt;/tr&gt;
     &lt;/thead&gt;
     &lt;tbody&gt;
        &lt;tr th:each=&quot;as : ${getAllAttendanceIn}&quot;&gt;
           &lt;td th:text=&quot;${as.checkIn}&quot;&gt;&lt;/td&gt;
        &lt;/tr&gt;
     &lt;/tbody&gt;
 &lt;/table&gt;

Can someone told me what happened and how to solve this? Many Thanks

答案1

得分: 1

It looks like the timezone where the application is executing is probably UTC-7. Then it converts it to UTC to get the date, which causes the date time to be displayed with +7 hours:

      Date in = Date.from(ldt.toInstant(ZoneOffset.UTC));

Then the date is correct, it is just expressed in UTC because that line is converting explicitly to it.

If it is not what you expect then convert to a Date using the local timezone. It is not clear why you are using java.time classes just to get the current date time if you are not really interested in converting to UTC timezone. A simple Date in=new Date()

英文:

It looks like the timezone where the application is executing is probably UTC-7. Then it converts it to UTC to get the date, which causes the date time to be displayed with +7 hours:

      Date in = Date.from(ldt.toInstant(ZoneOffset.UTC));

Then the date is correct, it is just expressed in UTC because that line is converting explicitly to it.

If it is not what you expect then convert to a Date using the local timezone. It is not clear why you are using java.time classes just to get the current date time if you are not really interested in converting to UTC timezone. A simple Date in=new Date()

huangapple
  • 本文由 发表于 2023年5月20日 20:29:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定