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
Related
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
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());
I have a date get saved in the database in IST format.
Date nowDate = new Date();
Date dateBefore = new Date(nowDate.getTime() - 7* 24 * 3600 * 1000);
System.out.println("Datebefore-->"+dateBefore);
Here in the above code dateBefore is get saved in the database.
From the database I am taking the data long value and I have to convert this into Google DateTime
Date dateBefore12 = new Date(longvalue);
com.google.api.client.util.DateTime dd = new DateTime(dateBefore12, TimeZone.getTimeZone("UTC"));
Now for example the output will be in the 2014-07-17T05:23:28.857Z which I have to pass to the Google You tube API.
Now from the response I will take Google DateTime, let say 2014-07-17T05:23:28.857Z which I have to increment the 1 minute and then convert it into long and save into db.
Convert the google DateTime to long.
TimeZone utc = TimeZone.getTimeZone("UTC");
SimpleDateFormat f = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
String input = dd.toString();
GregorianCalendar cal = new GregorianCalendar(utc);
cal.setTime(f.parse(input));
cal.add(Calendar.MINUTE,1);
Date time = cal.getTime();
long longvalue1 =cal.getTimeInMillis();
Now I will saved the data and try to retrieve it. It gives me back 2014-07-16T23:54:28.857Z.
But I need the save date value which I have increment by one minute in the format of google DateTime.
SimpleDateFormat also uses a time zone, due to an internal Calendar object, which defaults to the local time zone. If you don't want to use that default time zone, then before you call the format's parse() method, you should call setTimeZone() on it:
f.setTimeZone(utc);
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.
I have to get a Date in type Date, not in String.
I have this code:
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
Date date1 = new Date();
String date = (formatter.format(date1));
// At this point I get the date in correct format i.e 05/24/11
Date todaysDate = (Date)formatter.parse(date);
// But after this I get the date in format : Tue May 24 00:00:00 EDT 2011
// whereas I Want to get the date like above i.e 05/24/11
// And in type Date, not in type String
If anyone could help, thanks
The Date object just represents a point in time and has no notion of a format (or time zone). If you print out a Date object it first converts it to a String using the default formatting of EEE MMM dd HH:mm:ss zzz yyyy. If you want a specific formatting when you print it or otherwise represent it as a String, you'll need to use a formatter just like you already have.
In other words, you want Date.toString() to return the same as DateFormat.format()? You could just do exactly that:
public class MyDate extends Date {
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
public String toString() {
return this.formatter.format(this);
}
}
But do you really want to mix up presentation (date format) with your data?
There is no problem here, you have a Date representing and can save it into the DB as it is now. If you print it to the console it gets formatted according the default rules, this is why you think it is different from what you need, but it has actually already the right value.
So just go ahead and put it into your DB.
Chances are that you DB will hold on getting a Timestamp, in this case you can create one:
Date d = ...
java.sql.Timestamp ts = new java.sql.Timestamp(d.getTime());
and save this one.