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()
Related
// For Date validation
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
String datechosen = dateText.getText().toString() ;
Date dateselected = simpleDateFormat1.parse(datechosen);
System.out.println(dateselected);
// For Time validation
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
String timechosen = timeText.getText().toString();
Date timeselected = simpleDateFormat.parse(timechosen);
Calendar cal = Calendar.getInstance();
cal.setTime(timeselected);
cal.add(Calendar.HOUR, noofhourselected);
cal.add(Calendar.MINUTE, noofminselected);
timeselected = cal.getTime();
System.out.println(timeselected);
I am working on converting the string which i have into the date and time format. For example, the string datechosen contain "26/10/2020". I am able to to convert it into date format and print it out.
But for the time string, i am unable to print them out. I am facing the error below:
Screenshot of the log message
But if i swap the position of the codes the other way round,
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
String timechosen = timeText.getText().toString();
Date timeselected = simpleDateFormat.parse(timechosen);
Calendar cal = Calendar.getInstance();
cal.setTime(timeselected);
cal.add(Calendar.HOUR, noofhourselected);
cal.add(Calendar.MINUTE, noofminselected);
timeselected = cal.getTime();
System.out.println(timeselected);
// For Date validation
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
String datechosen = dateText.getText().toString() ;
Date dateselected = simpleDateFormat1.parse(datechosen);
System.out.println(dateselected);
The time will be printed instead
These are the two input fields
You are setting date in wrong format for time. for cal.setTime(timeselected); setTime takes Date Refer java doc
Use same format as used for 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
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);
I must convert the actual date and time to millis and into other timezone GMT+3 (my timezone is GMT-2). I use this code but it return me hte time but into my timezone....why ?
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
cal.get(Calendar.DAY_OF_MONTH);
cal.get(Calendar.MONTH);
cal.get(Calendar.YEAR);
cal.get(Calendar.HOUR_OF_DAY);
cal.get(Calendar.MINUTE);
cal.get(Calendar.SECOND);
cal.get(Calendar.MILLISECOND);
long timez = cal.getTime().getTime();
You need to use SimpleDateFormat. Calendar always uses default configured timezone on your machine. Here is example on how to achieve this functionality with SimpleDateFormat.
Date date = new Date();
DateFormat firstFormat = new SimpleDateFormat();
DateFormat secondFormat = new SimpleDateFormat();
TimeZone firstTime = TimeZone.getTimeZone(args[0]);
TimeZone secondTime = TimeZone.getTimeZone(args[1]);
firstFormat.setTimeZone(firstTime);
secondFormat.setTimeZone(secondTime);
System.out.println("-->"+args[0]+": " + firstFormat.format(date));
System.out.println("-->"+args[1]+": " + secondFormat.format(date));
}
where arg[0] and arg1 are the two time zone.
Refer this LINK
The getTime() method return the same time. it has no relation with the timezone.
I am trying to get a calendar object set to GMT, but the getTime() always returns the time in GMT+1 (my current time). I have tried:
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("skeniver"));
They all apparently return GMT, because
cal.getTimeZone().getDisplayName()
returns "GMT+00:00"; but
cal.getTime().toString();
always displays the time in GMT+1.
Does anyone have any idea why this is happening?
You need to adjust for daylight savings. I'm not sure if this will help but it's code I use for adjusting any timezone to UTC in an app that's currently being used by a number of people around the world. I use Date instead of Calendar but it works...
Date dateTimeNow = new Date();
TimeZone tz = TimeZone.getDefault();
int currentOffsetFromUTC = tz.getRawOffset() + (tz.inDaylightTime(dateTimeNow) ? tz.getDSTSavings() : 0);
Date dateTimeNowUTC = new Date(dateTimeNow.getTime() - currentOffsetFromUTC);
If you want to in string then prefer the DateFormat or SimpleDateFormat for this
here is example
SimpleDateFormat sdf = new SimpleDateFormat(); // here you can also define your format of date for e.g. "dd/MM/yyyy z"
sdf.setTimeZone("GMT");
Calendar cal = Calendar.getInstance();
System.out.println(sdf.format(cal.getTime()));
Calendar.getTime() returns a Date object. In Java, a Date is just a holder to a long timestamp starting in the UNIX epoch.
To display a Date in a different TimeZone than the default, you can use a SimpleDateFormat.