Getting next Sunday's date - android

I'm using the following code to get the date of the upcoming Sunday. When my device language is English, it is working properly. But it is not when device language is changed to Spanish.
Eg : for English it gives 2015-11-22 but for Spanish it gives 2015-11-29.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getDefault());
calendar.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
calendar.clear(Calendar.MINUTE);
calendar.clear(Calendar.SECOND);
calendar.clear(Calendar.MILLISECOND);
// get start of this week in milliseconds
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
calendar.add(Calendar.DAY_OF_WEEK, 7);
Date currentTime = calendar.getTime();
date = dateFormat.format(currentTime);

Change declaration of your SimpleDateFormat to this:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd" , Locale.US);
It works for every language.

Actually, you get, set wrong day:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getDefault());
// get start of this week in milliseconds
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(Calendar.DAY_OF_WEEK, calendar.SUNDAY);
Date currentTime = calendar.getTime();
String date = dateFormat.format(currentTime);

Related

Getting exact time in ( HH:mm:sss a) format from milliseconds

I have to print time in my app in HH:mm:sss AM/PM from milliseconds which is 24 Hours later time .
so to get 24 hours later time to current time i used the code as
public Date roundToNext24Hour() {
Date date = new Date();
Calendar c = Calendar.getInstance();
c = new GregorianCalendar();
c.setTime(date);
c.add(Calendar.HOUR_OF_DAY, 24);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
return c.getTime();
}
now i convert this time in HH:mm:sss am/pm format
SimpleDateFormat df = new SimpleDateFormat("HH:mm:sss a");
timeString = df.format(Utility.roundToNext24Hour())
but problem is timestring is always returning time which is 12 hour later not 24 hour later.
please help..
Try below code.
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
String str = df.format(date);

How to fetch recent seven days from specific date

If the date is 2017-03-30 that i want to fetch the date from 2017-03-23 to 2017-03-30
I try to use this code let my String change to Date format
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dateParse = sdf.parse("2017-03-30");
then i'm stuck , cause i take the reference is get the current time like this
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -7);
//may be my dateParse should put here , but i don't know how to do
Date monday = c.getTime();//it get the current time
String preMonday = sdf.format(monday);
Is any one can teach me how to fetch these seven days ? Thanks in advance.
You can use the code below
SimpleDateFormatdateFormat = new SimpleDateFormat("dd MMM yyyy");
Calendar c = Calendar.getInstance();
String date = dateFormat.format(c.getTime());
c.add(Calendar.DATE, 7);
String date1 = dateFormat.format(c.getTime());
Parse the date:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = sdf.parse("2017-03-30");
First Solution 1) And then either figure out how many milliseconds you need to subtract:
Date newDate = new Date(myDate.getTime() - 604800000L); // 7 * 24 * 60 * 60 * 1000
Second Solution 2) Or use the API provided by the java.util.Calendar class:
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -7);
Date newDate = calendar.getTime();
Then, if you need to, convert it back to a String:
String date = dateFormat.format(newDate);
This answer is from here
EDIT:
If you need output as 2017-03-29 2017-03-28 2017-03-27 ...... 2017-03-23 then try below code
for(int i = 1; i <= 7; i++){
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -i);
Date newDate = calendar.getTime();
String date = dateFormat.format(newDate);
//here in date you can get all date from and output as 2017-03-29 2017-03-28 2017-03-27 ...... 2017-03-23
}
Hope you need this

How convert timestamp to date on android?

I want convert timestamp to date and set time in this day to 12.00.
How can I do this?
If I use this:
Date date = new Date(event.getActionDate()*1000);
I can't set hour or minutes to this date, becouse methods for this operations are deprecated.
Explanation:
I have timestamp -
1461924872 // Fri, 29 Apr 2016 10:14:32 GMT
I want change hour in this timestamp (10:14 to 00.00).
long timeInMillis = 1461877200000l;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeInMillis);
System.out.println("Date1:"+calendar.getTime().toString());
calendar.set(Calendar.HOUR_OF_DAY, 0); //For 12 AM use 0 and for 12 PM use 12.
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date date = calendar.getTime();
System.out.println("Date2:"+date.toString());
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();//get your local time zone.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
sdf.setTimeZone(tz);//set time zone.
String localTime = sdf.format(new Date(time) * 1000));
Date date = new Date();
date = sdf.parse(localTime);
private String getDate(String timeStampStr){
try{
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date netDate = (new Date(Long.parseLong(timeStampStr)));
return sdf.format(netDate);
}
catch(Exception ex){
return dateInStr;
}
}
let me know if it is working?

Change timestamp into another gmt+ timestamp

Here goes my code but getting the same timestamp after changing the timezone.
TimeZone timeZone = TimeZone.getTimeZone("GMT+11");
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTimeInMillis(Long.parseLong(classListDto.get(i)
.getClassStartTime()) * THOUSAND);
cal2.setTimeInMillis(Long.parseLong(classListDto.get(i)
.getClassEndTime()) * THOUSAND);
cal1.setTimeZone(timeZone);
cal2.setTimeZone(timeZone);
long startTimestamp = cal1.getTimeInMillis();
long endTimestamp = cal2.getTimeInMillis();
Try something like this, instead of setting the timezone in your Calendar instance.
TimeZone timeZone = TimeZone.getTimeZone("GMT+11");
Calendar cal1 = Calendar.getInstance();
cal1.setTimeInMillis(Long.parseLong(classListDto.get(i).getClassStartTime()) * THOUSAND);
Date tempDate = cal1.getTime();
SimpleDateFormat df = new SimpleDateFormat();
SimpleDateFormat df1 = new SimpleDateFormat();
df.setTimeZone(timeZone);
Date gmtDate = df1.parse(df.format(tempDate)); // Put this in a Try/Catch block
long startTimestamp = gmtDate.getTime();
Hope this helps! I know its crappy to use 2 SimpleDateFormat objects though.
You can see the timezone change in your code if you do a cal1.getField(Calendar.HOUR); instead of just reading the milliseconds. Calendar seems to store the millis only in terms of UTC: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html#getTimeInMillis()

Current date should add 12 hours more in android

In my application i want to add 12 hours with the current date and time,then i have to show it in this format "yyyy-MM-dd HH:mm:ss". I wrote the code but unable to add 12 hours. How can i do? please help.
My code is :
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
m_tvTrackEnd.setText(date1);
The Calendar class has the add method which you can use to add certain units.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 12);
Date date = cal.getTime();
String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
m_tvTrackEnd.setText(date1);
How about
Calendar cal = Calendar.getInstance();
Date date = cal.getTime()+12*60*60*1000;
String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
m_tvTrackEnd.setText(date1);
You should add the 12 hours to the Calendar object like this:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 12)
Date date = cal.getTime();
String date1 = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
m_tvTrackEnd.setText(date1);

Categories

Resources