Setting Date in google calendar event - java

I am trying to set start date of an event in google calendar but it keeps taking the wrong value.
The code
EventDateTime start = new EventDateTime();
EventDateTime end = new EventDateTime();
SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-DD'T'hh:mm:ss.SSS'Z'");
Date dt = new Date();
Date endDate = new Date();
dt = df.parse("2015-05-23T09:00:00.000Z");
endDate = df.parse("2015-05-25T09:30:00.000Z");
start.setDateTime(new DateTime(dt));
end.setDateTime(new DateTime(endDate));
But the variables 'end' and 'start' take the values "2014-12-28T09:30:00.000+05:30 " and "2014-12-28T09:00:00.000+05:30" respectively.
My Timezone is GMT+5:30 .

Because it should be: yyyy-MM-dd not YYYY-MM-DD
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
here you can find what symbols int SimpleDateFormat means:
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
and th big DD is day of the year, and the small dd is day of the month.

Related

Printing out datetime in a specific format in Java?

I want to print out datetime in java in a specific format. I have this C# code which prints out the datetime in this format.
DateTime value = new DateTime(2010, 1, 18);
Console.WriteLine(value);
Console.WriteLine(value == DateTime.Today);
The result is - 1/18/2010 12:00:00 AM
Now, I want to write a java code that prints out the datetime in the same format. I used the joda.time library. This is what I tried so far.
DateTime today = new DateTime();
System.out.println(today.toString(“yyyy-MMM-dd”));
How can I pass the year,month and day as the constructor in the DateTime in java and print out in the above format.
Approach 1: Using java.time.LocalDateTime. (Strongly Preferred)
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println(dtf.format(now)); //2016/11/16 12:08:43
Approach 2: Using java.util.Date.
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date)); //2016/11/16 12:08:43
Approach 3: Using java.util.Calendar.
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal)); //2016/11/16 12:08:43
If you need date in 24 hour system then use this approach
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date custDate = new Date();
System.out.println(sdf.format(custDate));
Please note in 24 hour system there is no need to show AM/PM.
If you want date in 12 hour system then use below approach
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Date custDate = new Date();
System.out.println(sdf.format(custDate));
"a" in the date format will help to show AM/PM.
Please import below classes for above code to work
java.text.SimpleDateFormat
java.util.Date
LocalDate.of(2010, 1, 18).atStartOfDay().format(DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss a"))
or
LocalDate.of(2010, 1, 18).atTime(12, 0, 0).format(DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss a"));
if you want to add the time too
Please try to this one
public void Method(Datetime time)
{
time.toString("yyyy-MM-dd'T'HH:mm:ss"));
}

Java Date format weird

I have an issue where I would like to get the start of the day, however it seems to be setting it to 12:00 via automatic.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String todayDate = dfFull.parse("2014-06-06 00:00:00");
today = dfFull.format(todayDate);
System.out.println(today);
Why is this spitting out:
2014-06-06 12:00:00
The 12:00:00 is the issue
That is because hh represents the hour in 12 hour format. You need to use HH instead.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Have a look at the docs for more info.
Also, on a side note, there is a typo in your code.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date todayDate = dfFull.parse("2014-06-06 00:00:00"); // todayDate must be of type Date and not String
String today = dfFull.format(todayDate); // today should be of type String
System.out.println(today);
You should use HH for hours in this case.
SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String todayDate = dfFull.parse("2014-06-06 00:00:00");
today = dfFull.format(todayDate);
System.out.println(today);
Now you will get the out put as
2014-06-06 00:00:00
And again if you use hh that mean you are using 12 hour format while HH means 24 hour format
So

Java Date, string convert to Date Object

I have a question, how can I convert a string like 20130706123020 to a date object.
So I what to convert the string 20130706123020 to a date object looking like:
2013-07-06 12:30:20
Attempted code:
String date = "20130706234310";
Date date1 = new SimpleDateFormat("yyyy-m-d H:m:s").parse(date);
System.out.println(date1);
Any suggestions will be appreciated.
Thank you!
You have to first parse the String using the parse method from SimpleDateFormat.
Then pass the Date object returned by the parse method to another SimpleDateFormat and then using the format method get the date in the format you want.
String s = "201307061230202";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS"); // format in which you get the String
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // format in which you want the date to be in
System.out.println(sdf1.format(sdf.parse(s)));
The significance of HH, hh, KK and kk in the hour field is different. I have used HH you can use the one according to your requirement.
H Hour in day (0-23)
k Hour in day (1-24)
K Hour in am/pm (0-11)
h Hour in am/pm (1-12)
Use SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
Date date = sdf.parse("20130706123020");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf2.format(date));
use this :
long int my_date = 20130706123020L
and after that :
String date_Text = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(my_date));

Current Date & Time in Java

I want the current date and time in the following format :
Date :YYYYMMDD
Time : HHMMSS
I tried the following
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
//get current date time with Date()
Date date = new Date();
System.out.println(dateFormat.format(date));
//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
System.out.println(new Date().getTime());
By this I am getting the desired date output but the time is coming in this way 1341837848290.
The expected is HHMMSS.
Use format()
System.out.println(new SimpleDateFormat("HH:mm:SS").format(new Date()));
Date instance doesn't have any property to hold custom format, So you need to format the date instance to String with your custom format HH:mm:SS (See API doc for more detail)
See
IDEOne demo
try this
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
//get current date time with Date()
Date date = new Date();
System.out.println(dateFormat.format(date));
//get current date time with Calendar()
DateFormat timeFormat = new SimpleDateFormat("HHmmss");
Date d=new Date();
System.out.println(timeFormat.format(d);
Did you check out the joda-time library? Link here
With joda-time, you could easily call new DateTime(), call toString() on it and have this output, which may be more what you want:
public static void main(final String[] args) {
final DateTime d = new DateTime();
System.out.println(d.toString());
}
Output: 2012-07-09T14:54:13.366+02:00
Joda-Time is very powerful on the plus side. Of course, this is an extra lib you need to include, and if this is not possible or desired, another approach would probably be better.
I tried this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmmss");
Date date = Calendar.getInstance().getTime();
System.out.println(sdf.format(date));
Yields:
20120709 145518
First section is the date (20120709), the second section is the time(145518).
It seems that you have been using the wrong notation. I would recommend you take a look here for full details.
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:SS");
//get current date time with Date()
Date date = new Date();
System.out.println(dateFormat.format(date));
for more formatting refer API Doc

Java convert date to EST timezone to respecting DST

I want to get the current date converted to America/Montreal timezone. I'm doing it like this:
Date date = new Date();
TimeZone timeZone = TimeZone.getTimeZone ("America/Montreal");
Calendar cal = new GregorianCalendar(timeZone);
cal.setTime(date);
String whatIWant = "" + cal.get(Calendar.HOUR_OF_DAY) + ':'+
cal.get(Calendar.MINUTE)+ ':'+ cal.get(Calendar.SECOND);
log.info(whatIWant);
The conversion is just fine but I was wondering how robust this code is. What will happen when in no daylight saving?
That code is fine. Java automatically takes winter time or summer time into account.
You could also do this by using a DateFormat object to convert the date to a string, setting the desired time zone on the DateFormat object:
Date date = new Date();
DateFormat df = new SimpleDateFormat("HH:mm:ss");
// Tell the DateFormat that you want the time in this timezone
df.setTimeZone(TimeZone.getTimeZone("America/Montreal"));
String whatIWant = df.format(date);

Categories

Resources