How to Convert solr date to javascript date? - java

i am having the time in "2016-11-17T09:22:24Z" and i need to convert it into
"2016-11-1709:22:24".Just need to remove T and Z from solr Date and i need to add 330 minutes to that date and display it

First we can construct a javascript date object from the string and then you can convert it into correct format:
var dt = new Date('2016-11-17T09:22:24Z')
var formattedDate = dt.toISOString().substring(0, 19).replace('T', '')
console.log(formattedDate) should log 2016-11-1709:22:24
Or in one line:
new Date('2016-11-17T09:22:24Z').toISOString().substring(0, 19).replace('T', '')
will render: 2016-11-1709:22:24
Some more good discussions here: Convert javascript to date object to mysql date format (YYYY-MM-DD)

var userdate = new Date("2009-1-1T8:00:00Z");
var timezone = userdate.getTimezoneOffset();
var serverdate = new Date(userdate.setMinutes(userdate.getMinutes()+parseInt(timezone)));
This will give you the proper UTC Date and Time.
It's because the getTimezoneOffset() will give you the timezone difference in minutes. I recommend you that not to use toISOString() because the output will be in the string Hence in future you will not able to manipulate the date

Related

Converting Date format in Java to Date format in Swift

I have an endpoint in my Java program that returns a date variable of type Date. I'm calling this endpoint from a Swift program using Alamofire and receiving the response as a JSON object. the date that is getting returned is in the format: "2020-03-04 19:18:06.0" in Java. It gets received in my swift program as: "1583367486000"
I'm sure this is the seconds interval since a certain time period but how do I convert that to a Date format (lets say yyyy-mm-dd hh:mm:ss) in Swift?
Try the following.
let num = 1583367486000
let dateNum = Double(num/1000)
let date = Date(timeIntervalSince1970: dateNum)
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .gregorian)
//formatter.timeZone = NSTimeZone.local // for system clock's local time
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateStr = formatter.string(from: date) // 2020-03-05 09:18:06 +0000 => GMT

Problems converting a UNIX_TIMESTAMP from a SQL query into a Date in java

I have a MySQL query made in this way:
SELECT AVG(REALPOWER) AS REALPOWER,
`OBJECTID`,
UNIX_TIMESTAMP(LASTUPDATE)/(30*60) AS LASTUPDATE
FROM POWER
GROUP BY UNIX_TIMESTAMP(LASTUPDATE)/(30*60),
OBJECTID
To get my rows grouped by 30 minutes intervals.
Then in Java i want to convert LASTUPDATE to a Date.
The problem is i cannot understand how to dwell with LASTUPDATE, which has values like these:
823945.7650000000, 823945.7705555550, 823945.7761111110
Even multiplying them to 1000 to get millis gives me no real value to get a date...
double last = Double.parseDouble(rs.getString("LASTUPDATE"));
Date date = new Date((long) last*1000L);
A Date object in Java is just a point in time. Using the 7 and earlier API, if you want to actually see a formatted date, then you will have to use something like SimpleDateFormat. Here is an example:
double last = Double.parseDouble(rs.getString("LASTUPDATE"));
Date date = new Date((long) last*1000L);
SimpleDateFormat sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ENGLISH);
String dateFormatted = sdf.format(date);
System.out.println(dateFormatted);
Use floor() to convert the double value into an integer type. Something like:
floor(UNIX_TIMESTAMP(LASTUPDATE)/(30*60)) AS LASTUPDATE

Java util.numberFormatException for input string: "2014-01-12 05-44-56"

I'm new in OFBiz, and Java. I used bellow block of code for checking date time input and use that for searching in table.
Timestamp strtDate = UtilDateTime.getTimestamp((String)request.getParameter("strt_date"));
if(strtDate != null)
{
// then here i used the date for taking data.
}
When i fill the date time field of form to search or when no date is selected for searching error occure that show numberFormatException, so how i can solve that? thanks for any help and guide.
Based on the Apache ofbiz API it looks like UtilDateTime#getTimestamp(String) expects milliseconds value. You are passing in "2014-01-12 05-44-56". You need to parse your date first. With pure pre 1.8 java (keep in mind that formatters aren't thread safe):
String dateString = "2014-01-12 05-44-56";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date date = formatter.parse(dateString);
UtilDateTime.getTimestamp(date.getTime());
Since java 1.8 (highly recommended to switch if you can!):
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm-ss");
ZonedDateTime date = ZonedDateTime.parse(text, formatter);
long millis = date.toInstant().toEpochMilli();
you have to pass time in milliseconds not as you are passing.
you can check code also :
public static Timestamp getTimestamp(String milliSecs) throws NumberFormatException {
return new Timestamp(Long.parseLong(milliSecs));
}
it will parse the data in long which you are passing and that should be valid long value.
request.getParameter("strt_date") will anyways return String, so no need to cast it explicitly to String. Moreover, there will be a contract between Client & Server on the required Date Format. So you have to parse the String-Date in the same format using SimpleDateFormat. Code outlook will look like bellow:
SimpleDateFormat formatter = new SimpleDateFormat("contract-date-format");
Date date = formatter.parse(request.getParameter("strt_date"));
UtilDateTime.getTimestamp(date.getTime());

How to get Timestamp with AM/PM in java

I have a date as String , which needs to be converted in to Time Stamp with AM/PM . I tried the below way, I'm getting the proper date format but didn't get in AM/PM.
Can any one please help ?
code Snippet:
String dateString = "10/10/2010 11:23:29 AM";
SimpleDateFormat sfdate = new SimpleDateFormat("MM/dd/yyy HH:mm:ss a");
Date date = new Date();
date = sfdate.parse(dateString);
System.out.println(new Timestamp(date.getTime()));
Which gives me the output as below :
2010-10-10 11:23:29.0
But I needs it like this
2010-10-10 11:23:29.00000000 AM
Kindly help me please.
Why create a timestamp ? When you can just :
SimpleDateFormat sfdate = new SimpleDateFormat("MM/dd/yyy HH:mm:ss a");
Date date = new Date();
date = sfdate.parse(dateString);
System.out.println(sfdate.format(date) );
Output:
10/10/10 11:23:29 AM
Try:
System.out.println(sfdate.format(date));
As your last line rather than the one that you have at current.
Timestamp.toString() prints to a specific format: yyyy-mm-dd hh:mm:ss.fffffffff. The Timestamp object itself should be correct, if that's all you are looking for.
If you then want to define another format in order to print it as you like, that would require you to format Date object, using an appropriate pattern for the output format you are looking for.
What you're seeing is the result of Timestamp.toString(). The actual value in the Timestamp object instance is valid.
If you're getting an error in a subsequent SQL operation, please post that error along with the code you're using.

java.text.ParseException: Unparseable date when trying to convert "2013-07-01T04:37:14.771468Z" into Local time

I need to convert UTC time string I get into local time using following method,
String dateCreate = "2013-07-01T04:37:14.771468Z"
DateFormat dfParse = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss'Z'");
dfParse.setTimeZone(TimeZone.getTimeZone("UTC"));
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("Asia/Colombo"));
java.util.Date dateTime;
dateTime = dfParse.parse(dateCreate);
String dteCreate = df.format(dateTime);
Can someone plese give me a solution for this.? :)
EDIT: Now that I've checked it supports this easily, I'd strongly recommend that you use Joda Time. Its ISO-8601 parser works fine:
String dateCreate = "2013-07-01T04:37:14.771468Z";
DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
DateTime parsed = formatter.parseDateTime(dateCreate);
By default that will convert to the system default time zone, but you can change that behaviour with calls on DateTimeFormatter.
Joda Time is also a much cleaner API than the built-in one - you'll find any date/time code is easier to write and easier to read.
Look at your input data and your pattern:
String dateCreate = "2013-07-01T04:37:14.771468Z";
DateFormat dfParse = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss'Z'");
They don't match at all. You need something like:
// Don't use this directly!
DateFormat dfParse = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");
dfParse.setTimeZone(TimeZone.getTimeZone("UTC"));
Or:
// Don't use this directly!
DateFormat dfParse = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSX");
The latter will cope with any ISO-8601 time zone; the former restricts to UTC.
Unfortunately, the above will end up with the wrong number of milliseconds as it will take all the microseconds to be milliseconds. I don't know of a way of avoiding this in Java... you may need to trim the string first. For example:
// Remove the sub-millisecond part, assuming it's three digits:
int firstPartLength = "yyyy-MM-ddTHH:mm:ss.SSS".length();
String noMicros = dateCreate.substring(0, firstPartLength) +
dateCreate.substring(firstPartLength + 3);
// Now we've got text without micros, so create an appropriate pattern
DateFormat dfParse = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
Date date = dfParse.parse(noMicros);
Alternatively, if you know it's always going to end with "Z":
int firstPartLength = "yyyy-MM-ddTHH:mm:ss.SSS".length();
String noMicros = dateCreate.substring(0, firstPartLength);
DateFormat dfParse = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
dfParse.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = dfParse.parse(noMicros);
This is irritating, and it would be nice to be able to tell Java to treat any digits after the dot as "fractions of a second" but I don't know of any way of doing that using SimpleDateFormat. Note that you wouldn't be able to represent the sub-millisecond value using just Date anyway.
This is xsd dateTime format. You should use javax.xml.bind.DatatypeConverter for that
Calendar c = DatatypeConverter.parseDateTime(lexicalXSDDateTime);
Note that for SmipleDateFormat S means number of milliseconds so it will parse 771468 as 771468 ms not 0.771468 sec which adds extra 771 secs to the result date
Formatting part is OK

Categories

Resources