How to get a last Week date in GWT - java

I am Using GWT.
I need to retrieve the current date and and last week date. and pass it to GWT server using RPC.
How to retrieve the system date and last week date.??

You will Get Date/Time in/with GWT.
get a unix time stamp since the epoch
get year, month, today, date, hours, minutes seconds
//Get the browsers date (!!!note: I can't get GMT time zone in eclipse debugger)
Date date = new Date();
int Month = date.getMonth();
int Day = date.getDate();
int Year = date.getYear();
int Hour = date.getHours();
int min = date.getMinutes();
int sec = date.getSeconds();
int tz = date.getTimezoneOffset();
int UnixTimeStamp = (int) (date.getTime() * .001);//get unix time stamp example (seconds)
Long lTimeStamp = date.getTime(); //time in milleseconds since the epoch
int iTimeStamp = (int) (lTimeStamp * .001); //(Cast) to Int from Long, Seconds since epoch
String sTimeStamp = Integer.toString(iTimeStamp); //seconds to string
//get the gmt date - will show tz offset in string in browser, not eclipse debug window
String TheDate = date.toString();
//render date to root panel in gwt
Label label = new Label(TheDate);
RootPanel.get().add(label);
****** other wise Visit following link to get more information
1)a GWTInfo
2)one more a Stack
I hope it will help.

I make it using calculation by stander date util
you can find the date as below
Date fromday = new Date(System.currentTimeMillis() - 6000L * 60L * 60L * 24L);
Date today = new Date(System.currentTimeMillis());
Today will get current day, fromDay will get past 6 day

Related

How to get the timezone offset in android?

I found some similar questions, such as:
How to get the timezone offset in GMT(Like GMT+7:00) from android device?
How to find out GMT offset value in android
But all these answers(+12:00) are incorrect for New Zealand Daylight Saving Time now.
When I did debug, I got this from Google Calendar event object:
"dateTime" -> "2016-11-06T10:00:00.000+13:00"
So how to get the correct offset which should be +13:00?
Thanks.
To get the current offset from UTC in milliseconds (which can vary according to DST):
return TimeZone.getDefault().getOffset(System.currentTimeMillis());
To get a RFC 822 timezone String instead, you can simply create a SimpleDateFormat instance:
return new SimpleDateFormat("Z").format(new Date());
The format is (+/-)HHMM
So, I tried to get gmt offset through Calendar and SimpleDateFormat but both returns 0. I found the solution using deprecated methods in Date class.
So, this code works for me.
private double getOffset() {
Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
int defHour = calendar.get(Calendar.HOUR_OF_DAY);
int defMinute = calendar.get(Calendar.MINUTE) + (defHour * 60);
Date date = new Date(System.currentTimeMillis());
int curHour = date.getHours();
int curMinute = date.getMinutes() + (curHour * 60);
double offset = ((double) curMinute - defMinute) / 60;
return offset > 12? -24 + offset : offset;
}
Then you can format a result
This code return me GMT offset.
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault());
Date currentLocalTime = calendar.getTime();
DateFormat date = new SimpleDateFormat("Z");
String localTime = date.format(currentLocalTime);
It returns the time zone offset like this: +0530

Java get a current time in 24h format

Now i getting a current time
Calendar c = Calendar.getInstance();
int hours = c.get(Calendar.HOUR);
int minutes = c.get(Calendar.MINUTE);
int seconds = c.get(Calendar.SECOND);
and hours is always retrieving in 12h format. How i can change my code to getting actual hours in 24h format?
Use Calendar.HOUR_OF_DAY that is used for the 24-hour clock.
int hours = c.get(Calendar.HOUR_OF_DAY);

Can't get the number of days between 2 months - specific case

I have few dates in this format: 31/08/13 and I'm getting tehm from an xls file
What I need to do is to get the previous month and to calculate the days betwen these dates. I really don't know what to do.
Here is what I have tried:
code edited*
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
end.setTime(row.getCell(0).getDateCellValue());
start.add(start.MONTH,-1);
Date startDate = start.getTime();
Date endDate = end.getTime();
long startTime = startDate.getTime();
long endTime = endDate.getTime();
long diffTime = endTime - startTime;
long diffDays = diffTime / (1000 * 60 * 60 * 24);
if(diffDays < 0){
System.out.println(Math.abs(diffDays));
}
DateFormat dateFormatw = DateFormat.getDateInstance();
System.out.println("The difference between "+
dateFormatw.format(startDate)+" and "+
dateFormatw.format(endDate)+" is "+
diffDays+" days.");
But it seems wrong. I really can't get my mind on it as I'm so tired. I lost too many hours on this without luck.
I'm trying to get the same date but one month ago and to calculate the days, between these 2 dates endTime and startTime
Please help me!
I don't know what a ProdCalendar is, it's presumably something internal to your project. But the simple way to get the numbers of days between 2 dates, which is (I think) what you want, is this:
//First date
Date d1 = dateFormat.parse("31-10-13");
//Second date
Date d2 = dateFormat.parse("31-08-13");
//Interval:
long intervalMs = d1.getTime() - d2.getTime();
long intervalDays = intervalMs/(1000*60*60*24);
In your code, date22 is initialised just with new date() so it will be the current system time. You are comparing that (in a depracated way) to a date retrieved from the spreadsheet. I also notice that Calendar cal in this line near the start:
Calendar cal = Calendar.getInstance();
cal.setTime(row.getCell(0).getDateCellValue());
Is not used again, so appears to be redundant.
Your code is more complicated than it needs to be, so you need to step back and think about what you want to achieve. Simple code is good code.

Android date picker calculation , before and after a specific date

I have an application which use the current date and a date that is chose by user using date picker as follow:
If the date that the user chose is more than the current date +280 day,
some code will be executed.
If the date that the user chose is less than the current date , some
code will be executed.
I used this code to do so ..
Calendar start2 = Calendar.getInstance();
int birthYear = birthDayDatePicker.getYear();
int birthMonth = birthDayDatePicker.getMonth();
int birthDay = birthDayDatePicker.getDayOfMonth();
start2.set(birthYear, birthMonth, birthDay);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(birthDate);
cal2.add(Calendar.DAY_OF_MONTH,daysToAdd);
birthDayChosenCalender.set(birthYear,birthMonth,birthDay);
MaxBirthDayCalender.set(currentYear, currentMonth, currentDay);
long diff = birthDayChosenCalender.getTimeInMillis() - MaxBirthDayCalender.getTimeInMillis(); //result in millis
long daysBetween = diff / (24 * 60 * 60 * 1000);
System.out.println("Days between ,,,,,,,,,,,,,,,,,,"+daysBetween);
if(MaxBirthDayCalender.before(birthDayChosenCalender) && daysBetween <= 280){
do sth }
Is there any other clean way to do that ! because this way is not working well !
The other clean way to do it is to use the Joda Time library.
Other than that, everything can be done using millis and a single calendar instance:
Calendar pickedDate = new GregorianCalendar(
picker.getYear(),
picker.getMonth(),
picker.getDayOfMonth());
long pickedTime = pickedDate.getTimeInMillis();
long now = new Date().getTime();
if (pickedTime - now <= (280 * 24 * 60 * 60 * 1000)) { // 280 days in milliseconds
// ...
}
Should cover the requirement:
I have an application which use the current date and a date that is chose by user using date picker as follow:
If the date that the user chose is more than the current date +280 day, some code will be executed.
If the date that the user chose is less than the current date , some code will be executed.

Calendar.MINUTE giving minutes without leading zero

Hi all I am using below code to get android phone time, but it is giving me minutes without zero if the minutes are in between 1 to 9.
for example:right now I have time on my device 12:09 but its giving me as 12:9
Calendar c = Calendar.getInstance();
int hrs = c.get(Calendar.HOUR);
int mnts = c.get(Calendar.MINUTE);
String curTime = "" + hrs + ":" + mnts;
return curTime;
after above code I also try below code its giving same thing as above, minutes without zero before number it the minutes in between 1 to 9 . .
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
Date date = cal.getTime();
int mHour = date.getHours();
int mMinute = date.getMinutes();
As Egor said, an int is just an integer. Integers don't have leading zeros. They can only be displayed with leading zeros when you convert them to String objects. One way to do that is like this:
String curTime = String.format("%02d:%02d", hrs, mnts);
The format string %02d formats an integer with leading zeros (that's the 0 in %02d), always taking up 2 digits of width (that's the 2 in %02d).
That would produce the String
12:09
All is said about integer. But dealing with dates and Calendars to display that information should be used like so:
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yourpattern"); //like "HH:mm" or just "mm", whatever you want
String stringRepresentation = sdf.format(date);
the pattern "mm" will have a leading zero if it is between 0 and 9. If you use "mmmm" you'll get 0009, which doesn't look like it makes a lot of sense, but it all depends on what you want. :)
if you use pattern HH:mm you'll get 12:09 (the current time of your date instance).
You can format date using SimpleDateFormat class
if you are getting date and time from Calendar instance:-
final Calendar c = Calendar.getInstance();
SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm");
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
timeEdittext.setText(timeFormat.format(c.getTime()));
dateEdittext.setText(dateFormat.format(c.getTime()));
if you have day, month, year, hour, minute as integer(usually happens in datepicker and timepicker dialogs)
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, dayOfMonth);
String date = new SimpleDateFormat("dd-MM-yyyy").format(calendar.getTime());
Used below method to convert Hour:Minute in double format.
/*
* make double format hour:minute string
* #hour : 1
* #minute : 2
* return : 01:02
* */
public static String hourMinuteZero(int hour,int mnts){
String hourZero = (hour >=10)? Integer.toString(hour):String.format("0%s",Integer.toString(hour));
String minuteZero = (mnts >=10)? Integer.toString(mnts):String.format("0%s",Integer.toString(mnts));
return hourZero+":"+minuteZero;
}
function time()
{
var time = new Date();
var hh = time.getHours();
var mmm = time.getMinutes();
if(mmm<10)
{
mmm='0'+mmm
}
time = 'T'+hh+':'+mmm;
return time;
}

Categories

Resources