Tried to convert firestore timestamp to string and I got this!
I tried String date = FieldValue.serverTimestamp().toString(); and instead of time stamp I got this as shown in Screenshot1
The date inside a Firestore database must be stored as a Date object as explained here. Assuming that you have public getter named getDate() inside your model class, to print the date, please use the following code:
Date date = yourModelClass.getDate();
if (date != null) {
DateFormat dateFormat = SimpleDateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US);
String creationDate = dateFormat.format(date);
Log.d("TAG", creationDate);
}
What you are actually printing there is the address of the FieldValue class from the memory.
okay so the toString() will not output the date formatted since the object returned by the statement FieldValue.serverTimestamp() is a Date object so you can do something like this to handle date formatting.
Date now = FieldValue.serverTimestamp();
SimpleDateFormat dateFormatter = new SimpleDateFormat("E, y-M-d 'at' h:m:s a z");
Log.i("Format 1: ", dateFormatter.format(now));
Related
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"')
I have few string properties with custom type java.util.Date added in MainGenerator class.
In querybuilder how can I compare these strings with ge or le or gt or lt.
I save the db values in string type and I compare them like this
qb.queryBuilder().where(TestDao.Properties.Date_entered.ge(start)).list();
It doesn't work.
If you are using greenDao then in your MainGenerator you must be having the date as
testdao.addDateProperty("date_entered").notNull();
So in qb.queryBuilder().where(TestDao.Properties.Date_entered.ge(start)).list();
start should be java.util.Date.
Dates are persisted as timestamps of type long. Thus, for your query parameters, you should also use long values.
First Parse your date in String as you are saving date in database in string format. Then query data. Here is sample code.
SimpleDateFormat dateFormat;
Calendar calendar = Calendar.getInstance();
//Modify Calendar here according to your requirement.
dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
//Check if you have different date format then replace in above line.
String dateString = dateFormat.format(calendar.getTime());
//Then query your data
qb.queryBuilder().where(TestDao.Properties.Date_entered.ge(dateString )).list();
You can convert String date into milliseconds and can compare the values for your result:
public boolean checkDates(String date1, String date2) {
long milliDate1 = getMilliFromDate(date1);
long milliDate2 = getMilliFromDate(date2);
//Check date according to your requirement and condition
return milliDate1 < milliDate2;
}
public long getMilliFromDate(String dateFormat) {
Date date = new Date();
// "dd/MM/yyyy" this is date format i use you can use your own
//format which you are storing in local database like time stamp "yyyy-MM-dd HH:mm:ss"
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
try {
date = formatter.parse(dateFormat);
} catch (ParseException e) {
e.printStackTrace();
}
return date.getTime();
}
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));
I create a date and then format is like this:
Example 1:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
String currentDate = sdf.format(new Date());
What I would like to do is check if this date is before another date (also formatted the same way). How would I go about doing this?
Example 2:
Also, how would I check whether one of these is before another:
long setForLong = System.currentTimeMillis() + (totalTime*1000);
String display = (String) DateFormat.format("HH:mm:ss dd/MM/yyyy", setForLong);
EDIT:
I think more detail is needed. I create a date in two different ways for two different uses. The first use just formats the current date into a string so it is readable for the user. In the second case, I am using a date in the future with System.currentTimeMillis and adding on a long. Both result in a string.
Both methods format the date in exactly the same way, and I set the strings into a TextView. Later, I need to compare these dates. I do not have the original data/date/etc, only these strings. Becasue they are formatted in the same way, I though it would be easy to compare them.
I have tried the if(String1.compareTo(String2) >0 ) method, but that does not work if the day is changed.
If you only have two String objects that are dates available to you. You will need to process them in something, either in your own comparator class or in another object. In this case, since these are already formatted into dates, you can just create Date objects and compare using the methods previously posted. Something like this:
String string = "05:30:33 15/02/1985";
Date date1 = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy", Locale.ENGLISH).parse(string);
String string2 = "15:30:33 01/02/1985";
Date date2 = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy", Locale.ENGLISH).parse(string2);
if(date1.getTime()>date2.getTime()) {
//date1 greater than date2
}
else if(date1.getTime()<date2.getTime()) {
//date1 less than date2
}
else {
//date1 equal to date2
}
You should use Calendar for convenient comparing dates.
Calendar c1 = Calendar.getInstance();
c1.setTime(Date someDate);
Calendar c2 = Calendar.getInstance();
c2.setTime(Date anotherDate);
if(c1.before(c2)){
// do something
}
And you can format it at any time
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
String currentDate = sdf.format(c1.getTime());
I have a requirement that I need to compare two Dates. One Date will come from DB which is String in "YYYY-DD-MM" firm and I need to compare this String Date with current Date.
for this I am converting Date String into Date object.
Now I need current Date also in "YYYY-MM-DD" format and it should be Date object so that I can use.compareTo() method compare two dates..
Please help me how to do that...
Date cDate = new Date();
String fDate = new SimpleDateFormat("yyyy-MM-dd").format(cDate);
You can do it in following way
// pick current system date
Date dt = new Date();
// set format for date
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// parse it like
String check = dateFormat.format(dt);
System.out.println("DATE TO FROM DATEBASE " +
arrayOfStringDate[d].toString());
System.out.println("CURRENT DATE " + check);
// and compare like
System.out.println("compare "+
arrayOfStringDate[d].toString().equals(check));
Calendar c = Calendar.getInstance();
SimpleDateFormat tf = new SimpleDateFormat("yyyy-MM-dd");
String time=DB time;
Date parseTime= tf.parse(time);
Integer dayNow=c.getTime().getDate();
Integer dayDb=parseTime.getDate();
then you can compare dayNow and dayDb.
If your current date is actually an instance of the java.util.Date class, you don't need to specify a format for it; it's just a millisecond value that represents a specific moment in time.
You can get the current date like so:
Date currentDate = new Date();
You can use 2 ways:
DateFormat object. Use parse method.
Make your own parser of the Date. I mean, you convert the year, month and day in an integer each, and use Date constructor to get the Date.