Cant force Calendar to change its time - android

Im trying to get the current date and set the time on 20:00
I'm able to get it nu my code doesnt set the time, but also no errors. If i debug it hours is stille the current time. can you guys help me ?
Calendar cal = Calendar.getInstance();
String monthS = toString().valueOf(cal.get(Calendar.MONTH));
String dayS = toString().valueOf(cal.get(Calendar.DAY_OF_MONTH));
// int hourI = cal.get(Calendar.HOUR_OF_DAY);
// int minI = cal.get(Calendar.MINUTE);
String yearS = toString().valueOf(cal.get(Calendar.YEAR));
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd HH:mm yyyy");
try
{
cal.setTime(sdf.parse(monthS + " " + dayS + " " + "20:00" + yearS));
} catch (ParseException e)
{
e.printStackTrace();
}

Instead of MMM try M if months are X (1) or MM if months are XX (01).
MMM stands for Jan...Dec

Try this
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 20);
cal.set(Calendar.MINUTE, 00);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

I fixed it with this
Calendar cal = Calendar.getInstance();
long currentMil = cal.getTimeInMillis();
int hourI = cal.get(Calendar.HOUR_OF_DAY);
while (hourI <19)
{
currentMil = currentMil + ONE_HOUR;
hourI ++;
}
System.out.println(currentMil);

Related

How to set selecteddate in material-calendarview

I want to add selected date from data that i get from table. here is my array example
{"data":[{"Tanggal":"2019-07-02","Flag":1},{"Tanggal":"2019-07-03","Flag":0}]}
for real i don't know how to do it, my teacher is google, but i think i miss something. so i'm trying to write it manual like this
String date = "2019-07-02";
String parts[] = date.split("-");
int day = Integer.parseInt(parts[2]);
int month = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[0]);
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();
widget.setDateSelected(calendar,true);
but i get an error
widget.setDateSelected(calendar,true); // wrong 1st argument type
How can i achieve it , thanks in advance.
Btw, i using this https://github.com/prolificinteractive/material-calendarview
Use another method setSelectedDate(LocalDate date) from this library to set the date.
Here is the code...
try{
//Option 1
String myDate = "2019-07-02";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
Date date = dateFormat.parse(myDate);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
LocalDate ld = LocalDate.of(cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH) + 1,
cal.get(Calendar.DAY_OF_MONTH));
//Option 2
LocalDate ld1 = LocalDate.of(2019,07,02);
//and finally pass it in parameter
widget.setSelectedDate(ld);//you can also pass ld1
}catch (ParseException e){
e.printStackTrace();
}
And use below import for LocalDate
import org.threeten.bp.LocalDate;

Can't Set correct date and time to Calendar

I am getting date time as String in this format from the server:2017-11-15 16:27:02
I want to set Calendar with this time so I Do something like this:
String[] dateTime = response.body().getUser().getUserSessionExpire().split(" ");
String[] date = dateTime[0].split("-");
String[] time = dateTime[1].split(":");
Log.i("TIMMEE", "Received TIME: " + response.body().getUser().getUserSessionExpire());
int month = Integer.parseInt(date[1]);
int day = Integer.parseInt(date[2]);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, Integer.parseInt(date[0]));
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR, Integer.parseInt(time[0]));
calendar.set(Calendar.MINUTE, Integer.parseInt(time[1]));
calendar.set(Calendar.SECOND, Integer.parseInt(time[2]));
For example, if I get
2017-11-15 16:27:02
Calendar will set for :
2017-12-15 16:27:02 Or 2017-11-16 16:27:02
Actually, Month or Day will change!
What is the problem?
Thankyou for your answers.
You do not need to split the date . SimpleDateFormat is responsible for formating . So you can parse the date and get Date object from it . See code below and read the SimpleDateFormat(linked above).
Calendar calendar=Calendar.getInstance();
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd H:m:s", Locale.getDefault());
try {
Date d = DATE_FORMAT.parse("2017-11-15 16:27:02");
calendar.setTime(d);// Use this calender
} catch (ParseException e) {
e.printStackTrace();
}

android get date of up coming Sunday

I am using following code to get date of coming Sunday. Code is working in some devices but in some devices it shows Sunday after coming Sunday. Please help. why it is not working in some devices.
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar c=Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
c.set(Calendar.HOUR_OF_DAY,0);
c.set(Calendar.MINUTE,0);
c.set(Calendar.SECOND,0);
c.add(Calendar.DATE,7);
Date currentTime = c.getTime();
date = dateFormat.format(currentTime);
} catch (Exception e) {
e.printStackTrace();
}
private static Calendar getNextSundayDate() {
Calendar calendarForNextSunday = Calendar.getInstance();
int today = calendarForNextSunday.get(Calendar.DAY_OF_WEEK);
//System.out.println("today" + today);
if (today != Calendar.SUNDAY) {
int offset = Calendar.SATURDAY - today + Calendar.SUNDAY;
//System.out.println("offset" + offset);
calendarForNextSunday.add(Calendar.DATE, offset);
//System.out.println("new" + calendarForNextSunday.get(Calendar.DAY_OF_WEEK));
//System.out.println("next sunday" + calendarForNextSunday.get(Calendar.DATE));
}
return calendarForNextSunday;
}
This might work for you
may be the problem of TimeZone ?
try this:
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("some time zone"));

How to I get the list of week days for a specific date in android

I'm implementing the material calendar view in android app,the user can select a particular date in the calendar.how do i get the complete list of week days for that particular selected date
for example: if user selects 22 as the date,how do i get the week days in that row from calendar in which 22 is present
Your requirement is unclear. If you want to get the week day for that day, you can use
Calendar.getInstance().set(year, month, day);
int dayOfWeek = Calendar.getInstance().DAY_OF_WEEK;
Ref : https://developer.android.com/reference/java/util/Calendar.html
try this
public String[] getWeekDaySelected(String selectedDateStr) {
Calendar cal = Calendar.getInstance();
try {
Date startDate = sdf.parse(sdf.format(cal.getTime()));
Date endDate = sdf.parse(selectedDateStr);
weekDaysCount = Functions.getWeeksBetween(startDate, endDate);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String[] days = new String[7];
// set the011, 10 - 1, 12);
String[] arr = selectedDateStr.split("-");
cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(arr[2]));
cal.set(Calendar.MONTH, (Integer.parseInt(arr[1]) - 1));
cal.set(Calendar.YEAR, Integer.parseInt(arr[0]));
Calendar first = (Calendar) cal.clone();
first.add(Calendar.DAY_OF_WEEK, first.getFirstDayOfWeek() - first.get(Calendar.DAY_OF_WEEK));
first.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
// and add six days to the end date
Calendar last = (Calendar) first.clone();
last.add(Calendar.DAY_OF_YEAR, 6);
// print the result
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(df.format(first.getTime()) + " -> " +
df.format(last.getTime()));
for (int i = 0; i < 7; i++) {
days[i] = format.format(first.getTime());
first.add(Calendar.DAY_OF_MONTH, 1);
}
return days;
}

Date and time format in Android

How do you format correctly according to the device configuration a date and time when having year, month, day, hour and minute? for example I want to display 29 July, 2015, 10:30 Am, according to my time zone
You can use this method to format the datetime... u can replace new java.util.Date() with any datetime variable...
android.text.format.DateFormat df = new android.text.format.DateFormat();
df.format("dd-MMM-yyyy hh:mm aa", new java.util.Date());
String strDateTime = "29 July, 2015, 10:30 Am";
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM, yyyy, hh:mm a");
strDateTime = sdf.format(Calendar.getInstance().getTime());
holder.txtTime.setText(strDateTime);
Please integrate the above code, that works fine for me.
Any help, do let me know.
Please Find the below solution
Calendar ci = Calendar.getInstance();
String AM_PM;
if(ci.get(Calendar.AM_PM)==0)
{
AM_PM ="AM";
}
else
{
AM_PM ="PM";
}
String CiDateTime = "" + ci.get(Calendar.DAY_OF_MONTH)+" "+
(ci.get(Calendar.MONTH) + 1)+","+
ci.get(Calendar.YEAR) + "-" +
+ "," +getCurrentTime()
+" "+AM_PM;
Call the method
private String getCurrentTime()
{
int hrsRight = 0;
Calendar c = Calendar.getInstance();
int hrs = c.get(Calendar.HOUR);
int min = c.get(Calendar.MINUTE);
if (hrs>12)
{
hrsRight = hrs - 12;
}
else
{
hrsRight = hrs;
}
return String.valueOf(hrsRight)+":"+String.valueOf(min);
}

Categories

Resources