I have a requirement to read data from rest API for each and every minute and save that data it into DB. I am facing some issue to implement the rest URL for the start date and end dates. As of now from the current date I am able to execute it, but when I specify the start date and end date then for every one minute in between the start date and end date I need to make a call to rest API.
Note : I am using #Scheduler annotation for this one.
Rest API URL is formed dynamically like below :
private Optional<String> buidURL() {
TimeZone timeZone = TimeZone.getTimeZone(Constants.TIME_ZONE);
Date d=DateUtils.addDays(new Date(), -1);
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(Constants.DATE_FORMAT);
format.setTimeZone(timeZone);
String jpQueryReqParam =appProperties.getRest().getReqParam();
String jpQuery = appProperties.getRest().getUri();
Optional<String> feed = null;
try {
feed = Optional.of(jpQuery + URLEncoder.encode(jpQueryReqParam.replace("$queryTimestamp", format.format(d)), StandardCharsets.UTF_8.toString()));
} catch (Exception e) {
logger.info("Error occured at buidURL() " + e);
}
return feed;
};
and then calling rest API :
Optional<String> uri = buidURLFromTo();
String url=uri.get();
ResponseEntity<String> feedResponse = restTemplate.exchange(url, HttpMethod.GET, getRequest(headers()), String.class);
Related
I have been using selenium webdriver and chrome and logs recently. But any timestamp values are coming back in a weird date time stamp format. I've search all over, and I cannot figure what it is. Furthermore, other values besides timestamp (like requestId or walltime) are also in new unknown formats. What format is this and how can I get it into a normal (MM DD YYYY HH:MM:SS..) format?
timestamp was 2484894.662632 around June 23rd 2021, 10:53:23.118
timestamp was 2486019.900761 around June 23rd 2021, 11:12:01.277
timestamp was 2581839.545059 around June 24th 2021, 13:49:09.354
Example:
"requestId":"30432.634","timestamp":87693.142713,"type":"XHR","wallTime":1624556888.229531}
Code snippet:
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.ALL);
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
flavorCapability.setCapability("goog:loggingPrefs", logPrefs);
driver.manage().logs().get(LogType.PERFORMANCE).getAll();
There is two way to get the desired result:
1) Simple way:
LogEntries entries = driver.manage().logs().get(LogType.PERFORMANCE);
for(LogEntry entry: entries){
System.out.println(entry.getTimestamp());
System.out.println(entry.getLevel());
System.out.println(entry.getMessage());
System.out.println(entry.toJson());
System.out.println(new Date(entry.getTimestamp()));
}
2) Second way to do it:
import org.json.JSONException;
import org.json.JSONObject;
LogEntries logs = driver.manage().logs().get("performance");
for (Iterator<LogEntry> it = logs.iterator(); it.hasNext();) {
LogEntry entry = it.next();
try {
JSONObject json = new JSONObject(entry.getMessage());
JSONObject message = json.getJSONObject("message");
String method = message.getString("method");
System.out.println(method);
if (method != null && "Network.responseReceived".equals(method)) {
JSONObject params = message.getJSONObject("params");
JSONObject response = params.getJSONObject("response");
JSONObject headers = response.getJSONObject("headers");
String timestamp = headers.getString("date");
String url = response.getString("url");
int status = response.getInt("status");
System.out.println("Response = " + response);
System.out.println("URL = "+ url);
System.out.println("Status Code = "+ status);
System.out.println("headers: " + response.get("headers"));
System.out.println("Timestamp: " + timestamp);
}
} catch (JSONException e) {
System.out.println(e.getMessage());
}
}
Ref: https://chromedevtools.github.io/devtools-protocol/tot/Network/
Note: Please provide the exact requirement, what exactly you want to get?
Subtracting the timestamp as seconds from the 3 datetimes you got these stamps, I could deduce that the timestamp means number of seconds that have passed since 16:38:25 +- 5 sec on the 25th of May 2021. All three timestamps agree that this is the origin.
Don't ask me why the origin is at that time. Maybe the computer booted at that time, or some number overflowed and started from 0 again.
Hello I am trying to store the birthdate of the user in database with the code below:
private void btnActionPerformed(java.awt.event.ActionEvent evt) {
String username = txtUserName.getText();
String password = txtPassword.getText();
String email = txtEmail.getText();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String birthdate = sdf.format(JDateChooser.getDate());
Users user = new Users();
user.setUserName(cin);
user.setPassWord(firstName);
user.setEmail(email);
user.setBirthDate(birthdate);
try {
int count = Users.getInstance().insert(user);
if(count == 1){
JOptionPane.showMessageDialog(null,"success");
reset();
}else{
JOptionPane.showMessageDialog(null,"Faild");
}
} catch (Exception ex) {
Logger.getLogger(AddNewPatient.class.getName()).log(Level.SEVERE, null, ex);
}
}
I got an error which says String connot be converted to Date in the line "user.setBirthDate(birthdate);"
Because the parameter birthdate is assigned as Date type in the encapsulation(setBirthDate)
is there any way to solve this issue, I am new in java programming and I am trying to improve my skills in java.
If this returns a Date:
JDateChooser.getDate()
And what you need is a Date, then don't convert it to a String. Just keep it as a Date:
Date birthdate = JDateChooser.getDate();
// later...
user.setBirthDate(birthdate);
Note that you can then also remove this line, since you're not using the variable it declares:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
In general you want to keep data types in their raw form pretty much as often as possible. Unless there's a specific need for something to be represented as a string (displaying it to the user, sending it over a serialized API of some kind, etc.) then just use the data as-is instead of converting it to something else.
After you get the date with JDateChooser.getDate(), you are immediately converting it to a string: sdf.format(JDateChooser.getDate());
You should store the returned Date from JDateChooser.getDate() as an actual Date object.
Date birthdate = JDateChooser.getDate();
Then you can use it in your other function directly:
user.setBirthDate(birthdate);
If you do need the date as a string for some other purpose (perhaps display to the user), you can store a formatted string version in a different variable:
String birthdateString = sdf.format(birthdate);
Otherwise, if you don't need a string version, you can delete the line where you create sdf.
I need to filter the records by date through JAVA class:
public static final String DATE_TIME = "DateTime"
Date dateNow = setTimeToZero(Calendar.getInstance().getTime());
String date = DATE_TIME_FORMAT.format(dateNow);
Request request = table.createRequest();
DATE_FILTER = XPathFilter.newFilter("date-greater-than(" + Root_Table_DateField.format() + '$date')");
request.setXPathFilter(DATE_FILTER);
request.setXPathParameter(DATE_TIME, date);
RequestResult = reqResult = request.execute();
The field I am trying to access is define as DateTime, but I don't want the time now, so I set the time to zero, so I can filter for all field with date greater than dd-MM-yyyT00:00:00:000
But it return a predicate error: PredicateException: Invalid XPath expresion ./dateTime = 26-06-2020T00:00:00:000 - Unexpected 'T00:00:00.000'
Any clue?
Thank you
Try to add the date formatted like that (xs:date("2020-06-26Z")
date-greater-than(xs:date("2004-12-25-12:00"), (xs:date("2020-06-26Z"))
Reference: https://www.w3.org/TR/xpath-functions-31/
I am trying to integrate a database with a web application that extracts event data from Google Calendar API which inputs the data into the database. The following code is identical to the Quickstart class provided by Google.
I basically want 'DateTime start' to be converted to 'long start'. I need the long value for SQL.
import com.google.api.client.util.DateTime;
// ...
DateTime now = new DateTime(System.currentTimeMillis());
Events events = service.events().list(calendarId)
.setTimeMin(now)
.setOrderBy("startTime")
.setSingleEvents(true)
.execute();
List<Event> items = events.getItems();
if (items.isEmpty()) {
System.out.println("No upcoming events found.");
} else {
System.out.println("Upcoming events");
for (Event event : items) {
DateTime start = event.getStart().getDateTime();
DateTime end = event.getEnd().getDateTime();
if (start == null) {
start = event.getStart().getDate();
}
System.out.printf("%s\n", start.toString());
Google has implemented the Rfc3339 parser in Google HTTP Client Library. You can try parsing it first and the use the DateTime.getValue() function to convert it into long.
You may also try using the DatetimeFormatter to format it to the way you want the value.
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
.withZone(ZoneId.of("UTC"));
public void convertDatetime() {
String timeStamp = "2019-05-24T11:32:26.553955473Z";
DateTime dateTime = DateTime.parseRfc3339(timeStamp);
long millis = dateTime.getValue();
String result = formatter.format(new Date(millis).toInstant());
Here is the code:
public String createDeploymentTask(String project_key, String summary, String description) throws ResponseException { //ADD FIELDS: DATE...
IssueRestClient issueClient = restClient.getIssueClient();
IssueInputBuilder iib = new IssueInputBuilder();
iib.setProjectKey(project_key);
iib.setSummary(summary);
iib.setIssueTypeId(new Long(10800));
iib.setDescription(description);
iib.setFieldValue("customfield_15031", new Timestamp(System.currentTimeMillis()));
IssueInput issue = iib.build();
BasicIssue issueObj = null;
try {
issueObj = issueClient.createIssue(issue).claim();
} catch (RestClientException e) {
throw new ResponseException(400, "Input is invalid (e.g. missing required fields, invalid field values, and so forth)\n" + e.getMessage());
}
System.out.println("Issue " + issueObj.getKey() + " created successfully");
return issueObj.getKey();
}
I get the exception:
com.atlassian.jira.rest.client.api.domain.input.CannotTransformValueException: Any of available transformers was able to transform given value. Value is: java.sql.Timestamp: 2018-07-20 17:20:06.65
What format should have the second parameter in
iib.setFieldValue("customfield_15031", new Timestamp(System.currentTimeMillis()));
?
BaseValueTransformer that is used by IssueInputBuilder doesn't accept any time or date related value. You will have to format date on your own and pass as a string value. The format is 2018-07-25 so you will have to use format yyyy-MMM-dd.