How to format logfile in YYYYMMDD format? - java

I need to get the date in the following format: YYYYMMDD
Have code which outputs below result.
[2023-02-13 11:05:03] [SEVERE ] sever message
System.setProperty("java.util.logging.SimpleFormatter.format",
"[%1$tF %1$tT] [%4$-7s] %5$s %n");
But I wanted to be like this [20230213 11:05:03] [SEVERE ] sever message

To change the format of the date to YYYYMMDD, you need to modify the value of the "java.util.logging.SimpleFormatter.format" property.
Try changing it to the following:
System.setProperty("java.util.logging.SimpleFormatter.format", [%1$tY%1$tm%1$td %1$tT] [%4$-7s] %5$s %n");
In this format, %1$tY will give you the 4-digit year, %1$tm will give you the 2-digit month, and %1$td will give you the 2-digit day. This way, the date will be displayed in the desired format.

Related

localdate parsing can't parse hours and minutes

I need to get the date from a String, the format is the following: uuuu-MM-dd HH:mm
The input has that format and I want the same format for my dates (year-month-day hour:minutes). When I did this:
LocalDate aux = LocalDate.parse(times.get(index),DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm "));
with this input: 2017-12-05 20:16
I get only this: 2017-12-05
I don't know why, I've tried a lot of formats and always lost the hours and minutes. Any idea?
LocalDate only has year, month, day fields. It cannot contain time of day data.
Use LocalDateTime instead.
LocalDateTime aux = LocalDateTime.parse("2017-12-05 20:16", DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm"));

Talend timestamp - Getting a zero value instead of timestamp

I am converting string to date using a tConvertType component in Talend.The data(source is in String) is getting loaded when I do the date pattern yyyy-mm-dd but when I try with timestamp yyyy-MM-dd HH:mm:ss then I am getting an error. The data from the source has the timestamp but then I am getting an error.
For Eg : I have the source as 2015-09-03 14:14:90 , since data is in string so used tconverttype and then for destination the data type is date. But if I use timestamp then I am getting an error of Unparseable date and if I change to yyyy-mm-dd then the data is coming as 2015-09-03 00:00:00 which is wrong
try
convert(timestamp,expression)
i had the same problem in the past and convert fixed that
Using the type date and date format "yyyy-MM-dd' 'HH:mm:ss" it works fine
I recommand you to use date format by pressing Ctrl+space and choose from the list.

Date Format Confusion

in my App User Selects Date like 2013-01-02
but in MySql DB the column format is like 02-Jan-2013 so want change the Date Format
my code is:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
SimpleDateFormat formatter1 = new SimpleDateFormat("dd-mmm-yyyy");
Date start_date = (Date)formatter.parse(GBRF.getStart_date());//GBRF.getStart_date() returns Form Data as 2013-01-02
String dt=formatter1.format(start_date);
Date sd=(Date)formatter1.parse(dt);
at the End sd print Date like this Wed Jan 02 00:01:00 IST 2013
i dont want that format..
just i want like 02-Jan-2013
give me an idea..
Thanks in Advance
You can format Date into String with SimpleDateFormat, after that if you try to print date instance it will invoke toString() method of Date class which has no change in output and you can't alter that output because it is coming from toString() implmentation
Note: in your format you need to use M for month (note capital M)
A Date object does not have a format in itself. It holds just a point in time. The format comes into play when you convert it to or from a String.
Also note that m in the format is used for minutes not months. So you probably want a format like dd-MMM-yyyy.
Try
System.out.println(formatter1.format(sd));
to get the date printed as you like it.
You need to change your date format first :
`SimpleDateFormat formatter1 = new SimpleDateFormat("dd-MMM-yyyy");`
In your case, if you just print dt, it should print date in the required format instead of converting it into Date object again i.e. sd
When you are printing sd, without any formatting it will print the default String implementation of Date object which is exactly what you are seeing as an output

How to correctly format date for amazon S3 post upload with joda-time in java?

On amazon's documentation for post uploads to S3 it tells me that I need to format the date for the expiration key in the policy document like this:
The expiration specifies the expiration date of the policy in ISO8601
GMT date format.
For example, "2007-12-01T12:00:00.000Z" specifies that the policy is not valid after
12:00 GMT on 2007-12-01. Expiration is required in a policy.
and I can't seem to figure out how to get joda-time to print print the date in the
correct format.
However I can't seem to get it to print in GMT without the -4:00 or whatever for the local time zone.
I tried using the custom formatter with no luck, can anyone give me a hint here?
You need to use the LocalDate and LocalDateTime representations. Check it out:
DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
System.out.print( formatter.print( new LocalDateTime() ) );
will print something like:
2012-05-22T12:09:22.988

Update/ Retrieve /Inserting date field

I am having difficulties while updating a date field into the Database. The field type in the DB is Date/Time.
Now, I am trying to update the field name "R_Date".
Currently, I am using the SQL Expression in my jsp"
UPDATE request SET request_date ='"+Request_Date+"'"; , But it is not accepting.
In the select statement I am using a normal select, I tried to use to_char or to_date, but it is not accepting the format of "DD-MMM-YYYY"
So, can you please help me to retrive/Update/Insert date field in the format of "DD-MMM-YYYY" the date field?
The normal practice to store a timestamp in the DB (thus, java.util.Date in Java side and java.sql.Timestamp in JDBC side) is to use PreparedStatement#setTimestamp().
Date requestDate = getItSomehow();
Timestamp timestamp = new Timestamp(requestDate.getTime());
preparedStatement = connection.prepareStatement("UPDATE request SET request_date = ?");
preparedStatement.setTimestamp(1, timestamp);
The normal practice to obtain a timestamp from the DB is to use ResultSet#getTimestamp().
Timestamp timestamp = resultSet.getTimestamp("request_date");
Date requestDate = timestamp; // You can just upcast.
To convert between java.util.Date and java.lang.String you normally use SimpleDateFormat:
// Convert from String to Date.
String requestDateAsString = "09-Aug-2010";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
Date requestDate = sdf.parse(requestDateAsString);
// Convert from Date to String.
String anotherDateAsString = sdf.format(someDate);
See also:
PreparedStatement tutorial
How to avoid Java code in JSP file (!!!)
I think you should use MON instead of MMM.
Have you tried something like:
UPDATE request
SET request_date = to_date('" + Request_Date + "', 'DD-MON-YYYY')
Hope you realize that as your statement stands (if it worked), it would update every row in the request table (not sure if that's your intention or not but I thought I'd point it out).
You need to check what date format you are trying to insert, and try using to_date method with appropriate format.
Following is referenced from : http://infolab.stanford.edu/~ullman/fcdb/oracle/or-time.html
Oracle's default format for DATE is "DD-MON-YY".
If you want to retrieve date in particular format you need to use :
TO_CHAR(<date>, '<format>')
Similarly if you need to insert/update date with input of date other than in standard format, you need to use :
TO_DATE(<string>, '<format>')
where the <format> string can be formed from over 40 options. Some of the more popular ones include:
MM Numeric month (e.g., 07)
MON Abbreviated month name (e.g., JUL)
MONTH Full month name (e.g., JULY)
DD Day of month (e.g., 24)
DY Abbreviated name of day (e.g., FRI)
YYYY 4-digit year (e.g., 1998)
YY Last 2 digits of the year (e.g., 98)
RR Like YY, but the two digits are ``rounded'' to a year in the range 1950 to 2049. Thus, 06 is considered 2006 instead of 1906
AM (or PM) Meridian indicator
HH Hour of day (1-12)
HH24 Hour of day (0-23)
MI Minute (0-59)
SS Second (0-59)

Categories