I'm trying to retrieve a list of events from a google calendar, using the Java api (jar version v3-rev9-1.7.0-beta)
This code works fine
Events e = service.events().list("primary").
setMaxResults(3).
execute();
where service is a Calendar object.
However, if I add a setTimeMin or setTimeMax parameter, like this
Date now = new java.util.Date();
Events e = service.events().list("primary").
setTimeMin(new DateTime(now)).
setMaxResults(3).
execute();
it returns a failure message, "Bad Request".
(note that as of this version, the setTime functions take a google DateTime object. I've also tried with the previous version of the jar, which takes a string, but received the same message).
So I was just wondering if anyone has successfully used these functions - perhaps they're not supposed to be called in this manner? Is there a way to get more detail back on the error?
Thanks :)
DateTime startTime = new DateTime(new Date(), TimeZone.getDefault());
Sorts the problem
I also encountered this. It seems the format of the DateTime.toString() or DateTime.toStringRfc3339() methods are incorrect as input to setTimeMin().
The DateTime.toString() format is:
2012-07-04T21:02:16.590
yyyy-MM-dd'T'HH:mm:ss.SSS (SimpleDateFormat notation)
The format which it expects seems to be close to xsd:datetime format (whatever that is):
2012-07-04T21:02:16Z (zulu, gmt)
2012-07-04T21:02:16-07:00 (mst, -7h)
2012-07-04T21:02:16-0700 (it also works without the colon in the timezone)
yyyy-MM-dd'T'HH:mm:ssZ (SimpleDateFormat)
Formatting can be done with a SimpleDateFormat:
SimpleDateFormat FMT_TIME=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String fromTimeStr=FMT_TIME.format(new Date());
Events evts = client.events().list(cal.getUid()).setTimeMin(fromTimeStr)
.execute();
Now, since I'm using the older API, I'm not sure how this would be done if the only method is setTimeMin(DateTime), but this should get you closer.
The Google documentation or source should mention this somewhere.
Related
We want to add days to the current date and format it in a specific way. This was solved in Groovy 2.4.13 and the following date manipulation works fine:
today = new Date()+90;today.format('yyyy-MM-dd HH:mm:ss.S');
Result: 2019-12-02 08:07:15.294
In Groovy 2.5.4 the same expression throws this exception:
groovy.lang.MissingMethodException: No signature of method:
java.util.Date.plus() is applicable for argument types: (Integer)
values: [90] Possible solutions: parse(java.lang.String),
split(groovy.lang.Closure), use([Ljava.lang.Object;),
is(java.lang.Object), wait(), clone() at
Script1.run(Script1.groovy:3)
I was able to reproduce this behaviour in "Groovy sandboxes" online:
Working fine here: groovy-playground (Version 2.4.1.5)
Failing here: groovyconsole (Version 2.5.7)
What is the working alternative in this case? I have read about a new Date API, but couldn't find the details about how to use it, with date manipulation (+ 90 days for example).
Take a look at TimeCategory
import groovy.time.TimeCategory
def theDate = use(TimeCategory){new Date() + 90.days}.format('yyyy-MM-dd HH:mm:ss.S')
I agree with Ole V.V.'s recommendations to use the new Date/Time API. Here is how you would write his Java sample in a more Groovy style.
// you can assemble aggregate types by left shifting the aggregates
// I'm not endorsing this approach, necessarily, just pointing it out as an alternative
ZonedDateTime now = LocalDate.now() << LocalTime.now() << ZoneId.of('Africa/Bamako')
// the plus operator is overloaded
ZonedDateTime in90Days = now + 90
// you can pass a String to format without needed a full DateTimeFormatter instance
println in90Days.format('uuuu-MM-dd HH:mm:ss.S')
While Groovy adds some further support for the old Java Date class, I still believe that you should not use it. It was always poorly designed and is now long outdated. Instead use java.time, the modern Java date and time API. I am sorry that I will have to trust you to translate from Java code.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.S");
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Africa/Bamako"));
ZonedDateTime in90Days = now.plusDays(90);
System.out.println(in90Days.format(formatter));
Output when running just now was:
2020-01-01 08:37:13.3
Please substitute your desired time zone if it didn’t happen to be Africa/Bamako.
Link: Oracle tutorial: Date Time explaining how to use java.time.
You can use Calendar to achieve that
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DATE, 90);
Date date = cal.getTime();
All steps must be separate and not in a single line.
I have dates in XML
<date>1980-03-07+03:00</date>
I use this data in my tests and i get it from XML, but in the web page it is displayed in dd.MM.yyyy form, so i have to convert it to that format. I have following method for this:
public String convertXMLDateToString(String xmlDate) throws ParseException, DatatypeConfigurationException {
return new SimpleDateFormat("dd.MM.yyyy").format(new Date(DatatypeConverter.parseDate(xmlDate).getTimeInMillis())) ;
}
but this is returning 06.03.1980 . It is probably a timezone issue - how to i get the right date? The reason for using strings is that these are webdriver tests that i am writing.
Its returning actually the correct date. Either your test data is corrupted or you have a bug in your application. However if you're sure, that there's no bug and test data isn't corrupted, you can set the correct time zone manually, by doing something like this:
Calendar calendar = DatatypeConverter.parseDate(xmlDate);
calendar.setTimeZone(TimeZone.getDefault());
return new SimpleDateFormat("dd.MM.yyyy").format(new Date(calendar.getTimeInMillis()));
I am having a hard time trying to make a web service client work. It is a XML RPC specification. I am using Apache WS XML-RPC library, which I find full of holes that causes problem due to Serialization. I have to send a Date parameter for the library to add the tags , however the web service expects it with the TZ, that means adding -0500 at the end of the Date object. If I dont send it as Date Object, it wont add the tags and it will fail. And when trying to do this:
DateFormat df = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ssZ");
String fecha = df.format(new Date());
Date date = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ssZ").parse(fecha);
And using parameter date, and it always sends it as
<dateTime.iso8601>20130517T20:30:33</dateTime.iso8601>
Can't find a way for it to send it as Date object in the format above but with the -0500 at the end. Any help would be appreciated.
Im having a little issue with parsing json date.
Here is what I would like to parse:
{"driver": "247","firstName": "XXXXX","lastName": "XXXXX","lastLatitudeUpdate": "5/21/2012 4:49:17 PM","suspended": "false","checkedin": "0"}
I am having trouble parsing "lastLatitudeUpdate" is it because there are spaces in between? Thanks in advance for the help.
Assuming you are on Android and therefore working with java (yes you don't mention that, only the tag in your question suggests it...)
Like mentioned here (and in various other places) you can parse a date in java using the SimpleDateFormat class:
SimpleDateFormat parserSDF=new SimpleDateFormat("M/d/yyyy h:m:s a");
Date d = parserSDF.parse(dateField,0);
Of course you have to first parse you json input with some library (e.g. standard library from json.org or Google gson) and then parse the string you'll get there for the field into a date.
Short answer: No, there is no way for the JSON engine to recognize a string as a Date object.
Long answer:
There is no 'date' type in JSON. However, this JSON is fine, the catch is that lastLatitudeUpdate will be parsed as a string. In order to convert this to a date you should try something like
var my_object= JSON.parse({"driver": "247","firstName": "XXXXX","lastName": "XXXXX","lastLatitudeUpdate": "5/21/2012 4:49:17 PM","suspended": "false","checkedin": "0"});
my_object.lastLatitudeUpdate= Date.parse(my_object.lastLatitudeUpdate)
This function will give a timestamp. However, you have to make sure the string is correctly recognized, you may have to do some extra work.
Some links for hints
http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
http://www.java-samples.com/showtutorial.php?tutorialid=406
How are you parsing the date? In Chrome this seems to work fine:
new Date("5/21/2012 4:49:17 PM");
Mon May 21 2012 16:49:17 GMT-0400 (US Eastern Daylight Time)
So I'm modifying one of the opensource Google I/O opensource applications (2010) and I'm getting the following error when trying to sync the app with using custom Google spreadsheet, same headings different data (appears to sync fine with the default Google spreadsheet)
"Sync error: Problem parsing timestamp: java.text.ParseException: Unparseable date: "null 2010 10:45am -0700"
This is the Java Code that's throwing the error
private static long parseTime(String date, String time) throws HandlerException {
final String composed = String.format("%s 2010 %s -0700", date, time);
try {
return sTimeFormat.parse(composed).getTime();
} catch (java.text.ParseException e) {
throw new HandlerException("Problem parsing timestamp", e);
}
}
Here's links to the information (Atom) which it is trying to parse:
My Data
https://spreadsheets.google.com/feeds/worksheets/0AmvmSNjQXtJFdE1lTlFxVXZCLUN0OFpqa3oyM2d4bEE/public/basic
Google Data
http://spreadsheets.google.com/feeds/worksheets/twd6syM493oFqIFWeIm8qGw/public/basic"
I can't figure out why I'm getting this error. Any help would be greatly appreciated.
Check the parameter date you pass into this method. It seems to be null, which obviously is not a valid date.
Because #henrik has already posted what your actual current problem is, I'm giving a few recommendations based on your posted code:
You're using sTimeFormat, which must be at least a static variable, and is probably final as well (and usually should be, in this context). However, you aren't following naming conventions -it should probably be named TIMESTAMP_FORMATTER (if the only thing you do is get an actual date, use TIMESTAMP_PARSER). Also, although you're not likely to be using multiple threads on an android device, please be aware DateFormat and SimpleDateFormat are NOT threadsafe - the standard practice is to contruct a copy for each use.
You're manually formatting the timestamp itself, before attempting to parse it. There's no point - you should be using the existing data (eiither from the xml directly, or the rendered html), and supplying a custom formatting string.
You're setting two parts of the dates, year and timezone. At minimum, that should be moved outside of the actual parse piece. You should probably write a setToCanonicalDate method or something that takes the output from your parsing and sets it to the valid year and timezone. And how far are you distributing that app? I live in the Pacific Timezone - your default won't do me any good.
This is how you can work with time and long:
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, dp.getDayOfMonth());
c.set(Calendar.MONTH, dp.getMonth());;
c.set(Calendar.YEAR, dp.getYear());
long l = c.getTime().getTime();