Sending DATE/TIMESTAMP from device to REST service - android

I am trying to send a date and time to my SQL oracle database through my REST service. However, the field in the SQL database is getting null.
Here is my code to get the date and time:
public void getCurrentDateandTime() throws ParseException {
Calendar c = Calendar.getInstance();
c.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
currentDateandTime = dateFormat.format(c.getTime());
CDAT = dateFormat.parse(currentDateandTime);
}
In my REST service, this field is specified as a date datatype, however my the temporal I am using is TIMESTAMP as I want to get the date and time.
When I use postman sending through test data like this:
"createdTimestamp": "2018-02-12T09:27:39"
It is being received and shown in my SQL database.
Why is the date I am sending from Android receiving null?

Try this buddy:
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = df.format(c.getTime());
And then send this formattedDate string. Make it string type and then parse your string as datetime object. Hope this works. Happy Coding :)

Most REST APIs stick with iso8601 format as a standard.
The java code would be like:
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
df.setTimeZone(tz);
String nowAsISO = df.format(new Date());
Then in Oracle to get back is:
to_timestamp('2018-02-14T00:00:00.000Z', 'YYYY-MM-DD"T"HH24:MI:SS.ff3"Z"')

Related

How to get user's timezone and convert time saved in database according to the user's timezone? Please see details

I'm developing an app in which I'm saving the time when the post was posted.
I'm getting that time by using this code:
DateFormat currentTime = new SimpleDateFormat("h:mm a");
final String time = currentTime.format(Calendar.getInstance().getTime());
Now, what I want is I want to get user's timezone and convert the time saved in database using his/her timezone to his/her local time.
I tried doing this using code:
public String convertTime(Date d) {
//You are getting server date as argument, parse your server response and then pass date to this method
SimpleDateFormat sdfAmerica = new SimpleDateFormat("h:mm a");
String actualTime = sdfAmerica.format(d);
//Changed timezone
TimeZone tzInAmerica = TimeZone.getDefault();
sdfAmerica.setTimeZone(tzInAmerica);
convertedTime = sdfAmerica.format(d);
Toast.makeText(getBaseContext(), "actual : " + actualTime + " converted " + convertedTime, Toast.LENGTH_LONG).show();
return convertedTime;
}
but this is not changing the time.
This is how I'm trying to convert time saved in database using above method (postedAtTime is the time which is getting retrieved from database):
String timeStr = postedAtTime;
SimpleDateFormat df = new SimpleDateFormat("h:mm a");
Date date = null;
try {
date = df.parse(timeStr);
} catch (ParseException e) {
e.printStackTrace();
}
convertTime(date);
Please let me know what's wrong in my code or if this is wrong way?
The time string you're storing is not sufficient to be able to change timezones after the fact (h:mm a is only hours, minutes and am/pm marker). In order to do something like this you need to either store the timezone the original timestamp was in or better yet store the time in a deterministic manner like always UTC.
Example code:
final Date now = new Date();
final String format = "yyyy-MM-dd HH:mm:ss";
final SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
// Convert to UTC for persistence
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
// Persist string to DB - UTC timezone
final String persisted = sdf.format(now);
System.out.println(String.format(Locale.US, "Date is: %s", persisted));
// Parse string from DB - UTC timezone
final Date parsed = sdf.parse(persisted);
// Now convert to whatever timezone for display purposes
final SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm a Z", Locale.US);
displayFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
final String display = displayFormat.format(parsed);
System.out.println(String.format(Locale.US, "Date is: %s", display));
Output
Date is: 2016-06-24 17:49:43
Date is: 13:49 PM -0400

Comparison of Date and Time

I have to get count of days which are past to the current day.I have list of days in arraylist.I got the list and I dont know how to compare?Can anyone help me?
This is the code I tried,
private void weeklylogeval(){
int i;
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dateFormatter.setLenient(false);
Date today = new Date();
String s = dateFormatter.format(today);
System.out.println("current date & time new:::"+s);
for(i=0;i<datetime.size();i++){
String daytime=datetime.get(i);
if(today.before(daytime))
}
}
Pls some one help me!
Try this code for date difference manipulation.
String fd=from_date;//date get from mysql database as string.
String td=to_date;//Today's date as string.
if(!fd.equalsIgnoreCase("") && !td.equalsIgnoreCase("")){
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat formatter;
Date frmdt=new Date(fd);
formatter = new SimpleDateFormat("dd/MM/yyyy");
String s1 = formatter.format(frmdt);
Date todt=new Date(td);
String s2 = formatter.format(todt);
Date frmdate = sdf.parse(s1);
Date todate = sdf.parse(s2);
if(frmdate.compareTo(todate)<=0) {
//do your stuff
} else {
// do your stuff
}
}
http://developer.android.com/reference/java/util/Calendar.html
This should be allot easier to use for your purpose
Edit:
Methods you can use:
boolean after(Object calendar)
Returns whether the Date represented by this Calendar instance is after the Date represented by the parameter.
boolean before(Object calendar)
Returns whether the Date represented by this Calendar instance is before the Date represented by the parameter.
Maybe you can construct a Date form the String you get from DB, and then use today.before(daytime) to compare them.
Date daytime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(datetime.get(i));

Incorrect Result: Formatting ISO-8601 Time And Extract 12 Hour Time

I am fetching Newsfeeds from the Facebook API using FQL which returns a "created_time" field as a UNIX Timestamp. I am converting that into, what I believe is a ISO-8601 timestamp using this piece of code:
String getInitialCreatedTime = JOFeeds.getString("created_time");
long finalTimeStamp = Long.valueOf(getInitialCreatedTime);
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
String finalCreatedTime = dateFormatter.format(new Date(finalTimeStamp * 1000L));
Now, from the String, finalCreatedTime I want to extract just the time in 12 Hour (AM/PM) format.
To that effect, I am using this code:
final String old_format = "yyyy-MM-dd'T'HH:mm:ssZ";
final String new_format = "EEE MMM d hh:mm aa yyyy";
String oldDateSring = finalCreatedTime;
SimpleDateFormat sdf = new SimpleDateFormat(old_format);
Date d = sdf.parse(oldDateSring);
sdf.applyPattern(new_format);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
And:
// GET THE TIME
SimpleDateFormat sdfTimeFormatter = new SimpleDateFormat("hh:mm aa");
String getTime = sdfTimeFormatter.format(cal.getTime());
feeds.setTime(getTime);
Log.e("TIME", getTime);
The result of the first code block is: 2012-12-14T04:30:03+0530
And the result from the // GET THE TIME block is 04:30AM when it should be 04:30PM.
I would appreciate any pointers on this. Perhaps, I am implementing it wrong?
EDIT: I might add, that timestamps that fall between 12.00 PM and 1.00 PM are handled properly and show PM as they should.
You have :
String getInitialCreatedTime = JOFeeds.getString("created_time");
long finalTimeStamp = Long.valueOf(getInitialCreatedTime);
// Note 8601 is written with 'HH'
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
So far, so good. But then you need to create a date from this.
Date date = new Date(finalTimeStamp * 1000L)
Then, you need to format what you need (and never EVER parse a date you just formatted. That makes no sense at all).
String finalCreatedTime = dateFormatter.format(date); // Not sure if you need this one
And
SimpleDateFormat sdfTimeFormatter = new SimpleDateFormat("hh:mm aa");
String getTime = sdfTimeFormatter.format(date);
feeds.setTime(getTime);

Get current date as String warning deprecated toGmtString() issue

I want to get the current date day/month/year min:sec in Android so I have used the follow code
String data="";
Calendar c = Calendar.getInstance();
data=c.getTime().toGMTString();
But Eclipse notify me that the method .toGMTString(); is deprecated.
How could I get the current date as formatted String avoiding the use of this deprecated method?
From the Android documentation:
This method is deprecated.
use DateFormat
Since the GMT string is represented as 22 Jun 1999 13:02:00 GMT, we can use SimpleDateFormat (subclass of the abstract DateFormat) like so:
SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
String asGmt = df.format(c.getTime()) + " GMT";
Might want to double-check that format, but this'll get you started. Have a look at this IDEOne code.
The method toGMTString() from the type Date is deprecated.
you can check this for different types of date formations.
In your case use
SimpleDateFormat dfDate = new SimpleDateFormat("dd/MMM/yyyy HH:mm:ss");
String data="";
Calendar c = Calendar.getInstance();
data=dfDate.format(c.getTime());
System.out.println(data);//==========> 17/Oct/2012 08:36:52
If you want to print month number instead of month name
use
SimpleDateFormat dfDate = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String data="";
Calendar c = Calendar.getInstance();
data=dfDate.format(c.getTime());
System.out.println(data);//==========> 17/10/2012 08:36:52

How to Compare system date with mydate in android 2.1?

in my Android application, i am taking date and time from database. but i am not able to get the date in the "Date" Format from the database into my application, the date is in string format, so i am not able to compare the system date to database date.
if i convert the system date into string then i am not able to update the date into the database in recurring case. i also want to update the database date if the system date and database date is matched.
how can i achieve this is android.
Thanks in advance.
You can convert String to Date like this:
String str = "12/12/1912";
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(str);
And back to String
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("Date is : " + formatter.format(date));
And Date has before and after methods and can be compared to each other.
By the way there is also a library called Joda, you can also check it out.
Try this code:
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("dd-MMMM");
formattedDate = df.format(c.getTime());

Categories

Resources