[JAVA] I was stuck with the handling of the time zone when formatting with SimpleDateFormat

Note that I got stuck when processing Response from API in Jmeter.

Fri Apr 4 00:00:00 UTC+0900 1902

To format such a date string to Date

new SimpleDateFormat("EEE MMM dd HH:mm:ss zZ yyyy", Locale.ENGLISH);

This.

Official SimpleDateFormat documentation

z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800

I wrote, but I couldn't think of a combination. The whole code is below.

import java.text.SimpleDateFormat;

String date = "Fri Apr 4 00:00:00 UTC+0900 1902";

// Original format to convert from
SimpleDateFormat formatFrom = new SimpleDateFormat("EEE MMM dd HH:mm:ss zZ yyyy", Locale.ENGLISH);

// Target format to convert to
SimpleDateFormat formatTo = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss zZ", Locale.ENGLISH);

// Parse original string, using original format
Date formatDate = formatFrom.parse(date);

// Convert to a target format
String newDate = formatTo.format(formatDate);

System.out.println(newDate);

reference: https://stackoverflow.com/questions/47165086/how-to-convert-datetime-in-jmeter-using-beanshell-sampler

Recommended Posts

I was stuck with the handling of the time zone when formatting with SimpleDateFormat
When creating the top page, I was stuck with the handling of images (super beginner)
I checked because the response was strange when debugging with Tomcat 8
Problems I was addicted to when building the digdag environment with docker
I got stuck when port forwarding with VBox
I checked the number of taxis with Ruby
[JavaScript] I can't get the response body at the time of error with axios (ajax)
The procedure I did when I prepared the environment of gradle + Java with VS Code (Windows 10)
I was entrusted with the design of the back end, so let's learn the endpoint design firmly
I was addicted to the record of the associated model
Webdrivers :: BrowserNotFound: Failed to find Chrome binary. When I was trying to test E2E with Docker + Rails for the first time, I got stuck in an error.
When I tried to scroll automatically with JScrollBar, the event handler was drawn only once.
What I was addicted to when introducing the JNI library
Set the time zone in the JVM of your Azure application
Specify the character code of the source when building with Maven
I was addicted to the setting of laradock + VSCode + xdebug
What I was addicted to with the Redmine REST API
The story I was addicted to when setting up STS
When I was worried about static methods in java interface, I arrived in the order of name interpretation
What I was addicted to when updating the PHP version of the development environment (Docker) from 7.2.11 to 7.4.x
[Past POST] I will publish a part of the answer memo when I was a mentor of TECH :: CAMP
About the handling of Null
I tried to solve the problem of "multi-stage selection" with Ruby
Check the actual date and time at parse with Java's SimpleDateFormat
What I did when the DB did not start with docker-compose up
What I was addicted to when implementing google authentication with rails
I tried to check the operation of gRPC server with grpcurl
The point of addiction when performing basic authentication with Java URLConnection
I was addicted to the API version min23 setting of registerTorchCallback
UnsupportedClassVersionError was output when I placed the .war file built with Eclipse on EC2 and executed it.
A story that I was addicted to twice with the automatic startup setting of Tomcat 8 on CentOS 8
When I tried to build an environment of PHP7.4 + Apache + MySQL with Docker, I got stuck [Windows & Mac]