Android DatePicker- GregorianCalendar - 00:00:00 UTC - getTimeInMillis() - android

How do I set the Time of the GregorianCalendar to 00:00:00 UTC?
Because the following returns a Date at 10:00pm:
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar cal = Calendar.getInstance(timeZone);
cal = new GregorianCalendar(year, monthOfYear, dayOfMonth ,0 ,0, 0);
editor.putString("auswahldatum", String.valueOf(cal.getTimeInMillis() / 1000))
.apply();
Log.i("Kalender", String.valueOf(cal.getTimeInMillis()));

Look at Calendar's Calendar.getInstance(timeZone) method it already return of new object of gregorian calendar. You can create it for example like this one:
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(timezone);
Or
Calendar cal = new GregorianCalendar(year, monthOfYear, dayOfMonth ,0 ,0, 0);
cal.setTime(date);
cal.setTimeZone(timezone);
Actually you must set time zone after time setting

Related

Android: Error converting type Date to Calendar

I am using realm to save a date but need to use the day and month of the date for later use so am converting the type Date to Calendar using:
public static Calendar toCalendar(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal;
}
Then on onCreate of a fragment:
Date testDate = new Date();
CharSequence s = DateFormat.format("MMMM d, yyyy ", testDate.getTime());
Log.d("Date", String.valueOf(s));
// This prints May 26, 2017
Calendar testCalendar = toCalendar(testDate);
Log.d("CALENDAR DAY TEST", String.valueOf(testCalendar.DAY_OF_MONTH));
// This prints 5, MONTH prints 2. Rather the expected 26 & 5.
The DAY_OF_MONTH and MONTH methods of testCalendar are returning the day as the 5th and month as the 2nd rather than the 26th and 5th.
Those are static ints used for tellling Calendar what type of data you want.
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_MONTH);

How to show a specific month in android CalendarView?

I want to show a specific month to user using android CalendarView. Please help me to implement this functionality. I wrote following code but it is not working.
CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView);
Calendar calendar1 = Calendar.getInstance();
calendar1.set(2016, 6, 1);
Calendar calendar2 = Calendar.getInstance();
calendar2.set(2016, 6, 30);
calendarView.setMinDate(calendar1.DATE);
calendarView.setMaxDate(calendar2.DATE);
Following output is coming when i am trying to run the app using above code.
Hint: What is the the value of Calendar.DATE? Documentation says 5, and so when you call this method, you are saying "5 milliseconds past Jan 01, 1970."
setMinDate(long minDate)
Sets the minimal date supported by this CalendarView in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.
Basically, that is a static field, and probably not the value that you want.
Perhaps you want getTimeInMillis() which returns the long for the value that you set the date at?
calendarView.setMinDate(calendar1.getTimeInMillis());
calendarView.setMaxDate(calendar2.getTimeInMillis());
As From my experience i always used Calender.set(Calendar.YEAR, year) method to set year and Calender.set(Calendar.MONTH, month) method to set month and Calender.set(Calendar.DAY,day) method to set day . So i am suggesting you to do same ,
String date = "1/6/2016";
String parts[] = date.split("/");
int day = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[2]);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
long milliTime = calendar.getTimeInMillis();
And now set the selected date in the calendar view by doing
mCalendarView.setDate (milliTime, true, true);

How can i get the String name of day on DatePicker

I'm developing a simple app that the user pick some date on DatePicker and return the current day name in some Toast message.
but when I'm trying to do that it has given me a wrong date.
my code:
final int day = datepicker.getDayOfMonth();
datepicker.getDrawableState();
int mounth = datepicker.getMonth();
mounth+=1;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, day);
String formatDay = new SimpleDateFormat("E").format(cal.getTime());
Toast.makeText(MainActivity.this, formatDay, 2000).show();
You're setting the day of month when you create the Calendar, but you aren't setting the month or year. You need to getMonth() and getYear() of the DatePicker and set those on the Calendar also. Do GregorianCalendar cal = new GregorianCalendar(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), 0, 0) instead of Calendar.getInstance(). With getInstance() you are getting a Calendar for the current time, then just changing the day.

Create date object from Date picker and Time Picker

I'm trying to create a date object in Android from Date picker and Time picker. I know how to do seperatly but when I want to create single Date object using both Date picker and Time picker
I tried this
DatePicker dp = (DatePicker)findViewById(R.id.datePickerDelayed);
TimePicker tp = (TimePicker)findViewById(R.id.timePickerDel);
Date myDbDate = new Date(dp.getYear(), dp.getMonth(), dp.getDayOfMonth(), tp.getCurrentHour(), tp.getCurrentMinute());
but no luck as it is deprecated. Any one can please point me to a resource?
try to use Calendar, i'm working with it
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, monthOfYear);
cal.set(Calendar.DATE, dayOfMonth);
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Date date = cal.getTime();

How to get date & time in 13 digit(epoch time) from day,month,year,hours and minutes?

I am working on an application in which I have got date and time from similar dialog.
I have all this variable of type integer.
Now I have to convert this all variables in to the epoch time.
It is very important for me.
Is there any way to do this?
You can use Calendar Class, you must instantiate a new Calendar Object, set MONTH,DAY,YEAR,HOUR,MINUTE and SECOND and then use getTime() method.
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR, hours);
cal.set(Calendar.MINUTE, minute);
int epoch_time = (int) cal.getTimeInMillis()/1000;
Use Calendar:
Calendar c = Calendar.getInstance();
c.set(year, month-1, date, h, min, sec);
long t = c.getTimeInMillis();

Categories

Resources