Google Calendar Api - convert DataTime to Calendar object - java

I am trying to convert this DataTime object to Calendar object.
DateTime start = event.getStart().getDateTime();
DateTime format: 2015-11-11T19:25:55.000Z
I need format: "yyyy/MM/dd HH:mm:ss"
How can I cut TimeZone and convert it to Calendar object?
Original Google Calendar Api:
private List<String> getDataFromApi() throws IOException {
// List the next 10 events from the primary calendar.
DateTime now = new DateTime(System.currentTimeMillis());
List<String> eventStrings = new ArrayList<String>();
Events events = mService.events().list("primary")
.setMaxResults(10)
.setTimeMin(now)
.setOrderBy("startTime")
.setSingleEvents(true)
.execute();
List<Event> items = events.getItems();
for (Event event : items) {
DateTime start = event.getStart().getDateTime();
if (start == null) {
// All-day events don't have start times, so just use
// the start date.
start = event.getStart().getDate();
}
eventStrings.add(String.format("%s (%s)", event.getSummary(),start));
}
return eventStrings;
}

You can use SimpleDateFormat to parse your 2015-11-11T19:25:55.000Z into an actual Date object. Once you've got that Date object, it's as easy as:
Calendar calendar = Calendar.getInstance();
calendar.setTime(your_date_object);
I hope this helps!

I'm trying do somthing with it, but its not work.
DateTime start = event.getStart().getDateTime();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String newDate = dateFormat.format(start);
Maybe someone know why i can't parse this data
DateTime start = event.getStart().getDateTime();
String NewDate = String.format("(%s)", start);
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ", Locale.getDefault());
cal.setTime(sdf.parse(NewDate));
All the time i have unparseable date exception.
I tried formats:
yyyy-MM-dd'T'HH:mm:ss.SSSZ
yyyy-MM-dd'T'HH:mm:ssZZZZZ
yyyy-MM-dd'T'HH:mm:ss.SSSZZ

This work for me:
SimpleDateFormat dtExibicao = new SimpleDateFormat("dd/MM/yyyy");
Date dtInicio = new Date(event.getStart().getDateTime().getValue());
String stInicio = dtExibicao.format(dtInicio);
If you wanna a java.util.Calendar you can use a #k3v1n4ud3 sample code:
java.util.Calendar calendar = java.util.Calendar.getInstance();
calendar.setTime(dtInicio);

Related

How to delete some character from respone in Java?

I have some string respone date like this:
2018-11-30 12:00:00
and i want just get the time, so i want like this:
12.00
and my question, can we delete some character like that?
Sounds like you want to use SimpleDateFormat so something like:
Date today = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("hh.mm");
String folderName = formatter.format(today);
Based on your comment try:
int index = time.indexOf(‘:’);
int start = index -2;
int end = index + 2;
String newTime = time.substring(start,end);
String apiDateString = "2018-11-30 12:00:00"; // in this case, your string from api
Date apiDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(apiDateString); // convert api date string to date
SimpleDateFormat formatter = new SimpleDateFormat("hh.mm");
String yourDateString = formatter.format(apiDate);
I think you want this.
Edited with little comment.
convert string to date. (apiDateString to apiDate)
create new format for what you want to form. In this case, 12.00
convert apiDate to your format. (apiDate -> 12.00)
You can try this
Calendar calendar = Calendar.getInstance();
SimpleDateFormat mdformat = new SimpleDateFormat("HH:mm:ss");
String strDate = "Current Time : " + mdformat.format(calendar.getTime());
display(strDate);
You can use SimpleDateFormat to get time and then replace : by .
I hope it can help your problem!
You can refer to this post.
Use SimpleDateFormat to format you date.
From String to Date
// Convert from string to date
String yourDate = "2018-11-30 12:00:00";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(yourDate);
//Use calender instead of date to getHours and minute, due to deprecated
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
calendar.get(Calendar.HOUR);
calendar.get(Calendar.MINUTE);
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)+"."+calendar.get(Calendar.MINUTE)); //12.0

Convert HTML Time into Java Time Object [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
I am getting startTime and endTime value from html5 input type. I am getting it in Servlet in String.
I want to convert it into Java Date Object so I can use methods like before and after for comparing Time.
String startTimeValue = request.getParameter("startTime");
String endTimeValue = request.getParameter("endTime");
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
Date startTime = sdf.parse(startTimeValue);
Date endTime = sdf.parse(endTimeValue);
sdf.format(startTime);
sdf.format(endTime);
System.out.println(endTime.before(startTime));
Any help would be appreciated.
I think you're able to do the parsing right this way :
String DateString = request.getParameter("date");
//SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date date = sdf.parse(myDateString);
Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
calendar.setTime(date); // assigns calendar to the given date
int hour = calendar.get(Calendar.HOUR);
int minute; /... similar methods for minutes and seconds
I am achieving it using the following Code Snippet
String startTimeValue = request.getParameter("startTime");
String endTimeValue = request.getParameter("endTime");
String[] time1 = startTimeValue.split(":");
Date date1 = new Date();
date1.setHours(Integer.parseInt(time1[0]));
date1.setMinutes(Integer.parseInt(time1[1]));
date1.setSeconds(0);
String[] time2 = endTimeValue.split(":");
Date date2 = new Date();
date2.setHours(Integer.parseInt(time2[0]));
date2.setMinutes(Integer.parseInt(time2[1]));
date2.setSeconds(0);
System.out.println(date2.after(date1));

How to create specific Date Output as String?

I have this class:
public class Period {
private Date startDate = new Date();
private String startTime = "10:00";
private Date endDate = new Date();
private String endTime = "23:59";
}
Now i need two string outputs "2014-01-08T10:00:00" and "2014-01-08T23:59:00" !
How i can create these two outputs by using the class Period? It is necessary to work with the class Period, which includes startTime and EndTime as Strings!
I only have this small code for using the class SimpleDateFormat:
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Thanks for helping me !
What #tobias_k is saying is this. This is your format
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'");
When you call .format to format the Date, it returns a String
String dateString = format.format(period.getStartDate());
Then concatenate it
dateString = dateString + period.getStartTime();
Try this: Using GregorianCalendar
Date startDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
GregorianCalendar cal = new GregorianCalendar();
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), 10, 00, 00);
System.out.println(dateFormat.format(cal.getTime()));
Update1
To get the string value in variable:
String str = dateFormat.format(cal.getTime());
System.out.println(str);

calender show results like date1<= results<date2

I used two calender for starting date and finishing as below,
date.Ic.add(Restrictions.between("islemZamani", date1, date2));
However result of this criteria has results of date1 and between of them,not include results of date2.I mean ,It shows date1<= results. I want date1<= results<=date2. So I tried SimpleDateFormat like this;
String tar1 = new String();
String tar2 = new String();
TimeZone tz = TimeZone.getTimeZone("Europe/Istanbul");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
dateFormat.setTimeZone(tz);
tar1 = dateFormat.format(date1);
tar2 = dateFormat.format(date2);
tar1 = tar1.substring(11, 18) + "000000";
tar2 = tar2.substring(11, 18) + "235959";
c.add(Restrictions.between("islemZamani", tar1, tar2));
Now It gives NullPointerException.How can i solve this problem? Do you suggest any different way from SimpleDateFormat? Thanka for any reply.
Use a string to manipulate date is too complicated and may case a loooot of problems.
Use only java.util.Date and/or java.util.GregorianCalendar.
Try this :
GregorianCalendar calendar1 = new GregorianCalendar();
calendar1.setTime(date1);
// Edit the calendar1 here if you want to
GregorianCalendar calendar2 = new GregorianCalendar();
calendar2.setTime(date2);
calendar2.add(GregorianCalendar.DAY_OF_YEAR, 1);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
TimeZone tz = TimeZone.getTimeZone("Europe/Istanbul");
dateFormat.setTimeZone(tz);
String tar1 = dateFormat.format(calendar1.getTime());
String tar2 = dateFormat.format(calendar2.getTime());
c.add(Restrictions.between("islemZamani", tar1, tar2));
But without the trace, I'm not sure this will solve your problem...
And, if it's possible, use the date instead of the string in your restriction.

Find Missing Dates Between Two Dates

I got a list of dates as String
for example date1->'11-11-2010' and date2->'12-01-2011'
I want to print all the dates between these two dates..
I tried to work with cal.add() but am not able to set my date1 to my cal.. if i do so i get null p
below code should do the trick for you.
String date1 = "11-11-2010";
String date2 = "12-01-2011";
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(format.parse(date1));
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(format.parse(date2));
Date currentDate = calendar1.getTime();
while(!currentDate.equals(cal2.getTime())){
calendar1.add(Calendar.DAY_OF_MONTH, 1);
currentDate = cal1.getTime();
System.out.println(currentDate);
}

Categories

Resources