Convert TestNG Start and Stop time to Postgres Without Time Zone - postgresql

I currently have some TestNG custom reporting code that works with the local MySQL database I have been testing against. The resulting database is postgres due to issues with latest MySQL versions in AWS, in trying to convert the format of TestNG millis over I have been encountering issues with the format which I can't seem to get one that works right.
My custom report code was using the following:
report.reporting.put("startDate", testResult.getStartMillis());
report.reporting.put("endDate", testResult.getEndMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
String startDbTime = sdf.format(report.get("startDate"));
String endDbTime = sdf.format(report.get("endDate"));
When I try some of the dateformatters I am receiving indexing errors like: DateTimeParseException: Text could not be parsed at index 4
I've used some various options like offset or instant with no success.
This is a collection of some of the options I have tried.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
String startDbTime = sdf.format(report.get("startDate"));
String endDbTime = sdf.format(report.get("endDate"));
// OffsetDateTime startDbTime = OffsetDateTime.parse(startRawDbTime);
// OffsetDateTime endDbTime = OffsetDateTime.parse(endRawDbTime);
// ZonedDateTime startDbTime = ZonedDateTime.parse(startRawDbTime);
// ZonedDateTime endDbTime = ZonedDateTime.parse(endRawDbTime);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
String startRawDbTime = sdf.format(report.get("startDate"));
String endRawDbTime = sdf.format(report.get("endDate"));
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDate startParse = LocalDate.parse(startRawDbTime,formatter);
LocalDate endParse = LocalDate.parse(endRawDbTime,formatter);
Long startTimeRaw = Long.parseLong(report.get("startDate").toString());
Instant startInst = Instant.ofEpochMilli(startTimeRaw);
ZonedDateTime zoneStart = ZonedDateTime.ofInstant(startInst, ZoneOffset.UTC);
LocalDate dateStart = formatter.format(zoneStart);
Is there a conversion step I am missing? I thought it would be simple to convert from millis to something that postgres would accept.

Worked out how to do this with PostGres by resetting the DB table to be a date with time zone, then adjusted my Java code to the following:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String startRawDbTime = sdf.format(report.get("startDate"));
String endRawDbTime = sdf.format(report.get("endDate"));
Timestamp timeStart = Timestamp.valueOf(startRawDbTime);
Timestamp timeEnd = Timestamp.valueOf(endRawDbTime);
With the statement insert updated with the right type:
reportInsert.setObject(4,timeStart, Types.TIMESTAMP);
reportInsert.setObject(5,timeEnd, Types.TIMESTAMP);
Everything works as expected now.

Related

Date Format For Google Classroom API ScheduledTime

I'm trying to set the scheduled time when creating an assignment using the Google Classroom API. However, I'm confused about which date format is needed. By the error messages, it seems to accept a string which holds a timestamp and a timezone or Z at the end. Among others, I've tried using System.currentTimeMillis() + "Z", as well as googleDate.getValue() + "Z", googleDate.getValue() since Google Date format seems to be the way to go based on this doc but none of them seem to work.
Any ideas perhaps?
Thank you.
String timezone = timestamp + offset + "";
System.currentTimeMillis()
com.google.api.client.util.DateTime googleDate =
new com.google.api.client.util.DateTime(new java.util.Date());
// Date javaDate = new Date(googleDate.getValue());
CourseWork courseWork = new CourseWork()
.setCourseId(course.getId())
.setTitle("title PUBLISHED 2")
.setDescription("desc")
.setScheduledTime(googleDate.getValue() + "Z")
.setMaxPoints(100.0)
.setDueDate(date)
.setDueTime(timeOfDay)
.setWorkType("ASSIGNMENT")
.setState("PUBLISHED")
;
This is what I get when I manually add a timestamp and turn it into a string.
And this using the Google date instead.
And this with the new Java 8 apis
java.time
I recommend that you use java.time, the modern Java date and time API, for your date and time work. The following code gives the same result as the code from your answer.
LocalDate localDate = LocalDate.now().plusDays(7);
String s = localDate.atStartOfDay(ZoneId.systemDefault())
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
System.out.println(s);
Output in my time zone today:
2021-10-20T00:00:00+02:00
Compared to your own answer you have fewer conversions, and you are freed from writing your own format pattern string since the formatter we need is built in.
This worked:
LocalDate localDate = LocalDate.now().plusDays(7);
java.util.Date date1 = java.util.Date.from(localDate.atStartOfDay()
.atZone(ZoneId.systemDefault())
.toInstant());
String s = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").format(date1);
It seems the imports were using the Google Date class instead of java.util.date.

MyBatis maps Timestamp in milliseconds

How can I map the timestamp result as regular timestamp, like this:
"2021-08-17T18:39:52.0000832-12:00"
already tried adding the jdbc = jdbc.Type.TIMESTAMP
An amazing solution found here:
Date and Timestamp serialization by Jackson ObjectMapper
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")

Unparseable date error format in Spring Boot Mongo DB

MongoCollection<Document> Profile_List = db.getCollection("Profile_List");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD");
Date todaydate = format.parse(new Date().toString());
ArrayList<Document> activeList=profile.find(Filters.regex("lastActive",todayDate.toString())).into(new ArrayList<Document>());
This is the code what we have written. We are getting an “Unparseable date error”. Can someone please help?
This is wrong:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD");
Date todaydate = format.parse(new Date().toString());
The expression new Date().toString() does not return a string that conforms to the format yyyy-MM-DD, so if you try to parse it as if it is formatted that way, you will get an exception.
If you want a Date object that represents the current date and time, simply do this:
Date todaydate = new Date();
No need to convert the Date object to a string and trying to parse it.
If you need a string with the current date in the format yyyy-MM-dd then do this:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String todaydate = format.format(new Date());
Note: You used DD in your date format string but you most likely meant dd. See the API documentation of SimpleDateFormat.
If you are trying to get the current date string in yyyy-MM-dd format. You can do format it like this
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateString = simpleDateFormat.format(new Date());

OffsetDateTime.parse('2018-12-03T18:07:55') throwing DateTimeParseException

I believe this should work. Granted it has no specified offset, but shouldn't that then default to UTC? And if not, how can I parse a string like this into an OffsetDateTime?
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd['T'[HH:mm:ss][.SSSSS]][z][x]");
String datetime = "2018-12-03T18:07:55";
OffsetDateTime odt = OffsetDateTime.parse(datetime, formatter);
Exception thrown:
java.time.format.DateTimeParseException: Text '2018-12-03T18:07:55' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2018-12-03T18:07:55 of type java.time.format.Parsed
As you say, it's failing because there's no offset in the format. To work around this, you can use the same format to parse to a LocalDateTime, and then combine with a ZoneOffset to create an OffsetDateTime:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd['T'[HH:mm:ss][.SSSSS]][z][x]");
String datetime = "2018-12-03T18:07:55";
LocalDateTime ldt = LocalDateTime.parse(datetime, formatter);
OffsetDateTime odt = OffsetDateTime.of(ldt, ZoneOffset.UTC);
System.out.println(odt);

How to modify the date?

I cannot get the add function in Blackberry Java.
// Date
private static DateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
private static Calendar cal = Calendar.getInstance();
public static final String date = dateFormat.format(cal.getTime())
.toString();
The cal variable don't have add function because I want to reduce 1 day from current date.
The source stated that used cal.add(Calendar.DATE, -1);.
The following solution works when using Java SE. I haven't verified using BlackBerry Java ME yet. But, given that I am only using functions that exist in both the SE version of Calendar and the BlackBerry version of Calendar, hence I have a good feeling about the accuracy of this solution. Append these lines to your code:
long curTime = cal.getTimeInMillis();
curTime -= 1000*60*60*24;
cal.setTimeInMillis(curTime);
System.out.println(dateFormat.format(cal.getTime()).toString());
Try this:
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(cal.getTime() - DateTimeUtilities.ONEDAY);
Updated, based on ecb0628's answer and paulkayuk's comment.
Check Calendar, getTimeInMilis(), setTimeInMillis(long millis), and DateTimeUtilities.ONEDAY.