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"));
Related
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;
}
I've a previous date from current date which is saved in database and need to get date 7 days next date. How can i get it?
For example:
i've date 1461560032085 milliseconds. How can i get 7 days next date?
It is very simple to use Calendar class
Calendar calendar = Calendar.getInstance();
calendar.setTime(your_current_date);
calendar.add(Calendar.DAY_OF_YEAR, +7);
Date newDate = calendar.getTime();
1 day = 86400000 milliseconds
So 7 days after "1461560032085" will be = 1461560032085 + 86400000 * 7
Hope this helps!
To calculate 7 days after the current day you should do the following:
nextWeek = yourdate + 7*24*60*60*1000
public static String getAdded_date(String previous_date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
try {
c.setTime(sdf.parse(previous_date));
} catch (ParseException e) {
e.printStackTrace();
}
c.add(Calendar.DAY_OF_WEEK, 7); // number of days to add, can also use Calendar.DAY_OF_MONTH in place of Calendar.DATE
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
String output = sdf1.format(c.getTime());
return output;
}
private int getaddedDate(int previousdate)
{
return previousdate + TimeUnit.DAYS.toMillis(7);
}
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);
I have a start date (day, month, year) and need the date say 4 weeks from that date. How can I calculate that? I know how to find the difference between two dates using Calendar so I assume I'm not too far from the answer... Thank you!!!
edit:
This is the final code I wound up using. It returns a String whose value is a date span formatted "MM/dd/YYYY - MM/dd/YYYY"
#SuppressLint("SimpleDateFormat")
private String getSessionDate(int position) {
MySession ms = mSessionList.get(position);
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Calendar calendar = Calendar.getInstance();
calendar.set(ms.getStartYear(), ms.getStartMonth(), ms.getStartDay());
Date startDate = calendar.getTime();
String durationString = ms.getDurationString(); // Format is "## weeks"
int i = 0;
while (Character.isDigit(durationString.charAt(i))) {
i++;
}
int weeks = Integer.parseInt(durationString.substring(0, i));
calendar.add(Calendar.WEEK_OF_YEAR, weeks);
return (format.format(startDate) + " - " + format.format(calendar.getTime()));
}
You can use Calender instance for that.
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentdate);
calendar.add(Calendar.DAY_OF_YEAR, no_of_days)
Date newDate = calendar.getTime();
You can calculate the date by adding or subtracting the no of days
Example :
Get date after 1 week
calendar.add(Calendar.DAY_OF_YEAR, 7);
Get date before 1 week
calendar.add(Calendar.DAY_OF_YEAR, -7);
Date date=null;
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
date = originalFormat.parse(strDate); // strDate is your date from which you want to get date after 4 weeks
} catch (Exception e) {
e.printStackTrace();
}
long timeFor4week=4*7*24 * 60 * 60 * 1000; /// here 24*60*60*1000 =24 hours i.e 1 day
long timeAfter4week=date.getTime()+timeFor4week;
String finalDateString=originalFormat.format(new Date(timeAfter4week));
So you can get day after 4 weeks.
Suppose my date is 02-01-2013
and it is stored in a variable like:
String strDate = "02-01-2013";
then how should I get the day of this date (i.e TUESDAY)?
Use Calendar class from java api.
Calendar calendar = new GregorianCalendar(2008, 01, 01); // Note that Month value is 0-based. e.g., 0 for January.
int reslut = calendar.get(Calendar.DAY_OF_WEEK);
switch (result) {
case Calendar.MONDAY:
System.out.println("It's Monday !");
break;
}
You could also use SimpleDateFormater and Date for parsing dates
Date date = new Date();
SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd");
try {
date = date_format.parse("2008-01-01");
} catch (ParseException e) {
e.printStackTrace();
}
calendar.setTime(date);
First split the string
String[] out = strDate.split("-");
s1 = Integer.parseInt(out[0]);
s2 = Integer.parseInt(out[1]) - 1;
yr = out[2];
char a, b, c, d;
a = yr.charAt(0);
b = yr.charAt(1);
c = yr.charAt(2);
d = yr.charAt(3);
s3 = Character.getNumericValue(a)*1000 +
Character.getNumericValue(b)*100 +
Character.getNumericValue(c)*10 +
Character.getNumericValue(d);
then create a calendar instance on that day
Calendar cal = Calendar.getInstance();
cal.set(s3, s2, s1);
then get the day
cal.get(Calendar.DAY_OF_WEEK);
use this format for date, day and time.
Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
and get out put of object here with format method.
ft.format(dNow)
I think that based on Android documentation is suggested the use of Calendar,
You need to be careful because the first day 1 is Sunday and the first month January
Also check that you can get DAY_OF_WEEK, DAY_OF_MONTH etc