How to post dateTime to soap webservice from android - android

When I post the datestring to server, the time always changes. How can I post the datestring in soap webservices.
<Validfrom>dateTime</Validfrom>
I convert the string to date object, it posted but the time changes.
plz help me anyone to me
private Date getDateObj(String dateInString) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm");
Date date = null;
try {
System.out.println(dateInString);
date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
Above code represents my date object and I posted data like:
request.addProperty(name.trim(),
getDateObj(value));

Actually you have to calculate server time like GMT because your location is India (according to stack overflow profile). So, you are in +5.30 GMT. That's why you are getting wrong time from server with India time. So, use my code that will give you GMT server time :)
Calendar cal = Calendar.getInstance();
Date currentLocalTime = cal.getTime();
DateFormat date = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
date.setTimeZone(TimeZone.getTimeZone("UTC-06"));
String localTime = date.format(currentLocalTime);
may thz will help u
best of luck dude

Related

How to get the date from "2019-06-27T12:30:00.000+0000"?

I want to get date and time from 2019-06-27T12:30:00.000+0000 in android. I tried a code but it is not working.
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date date = null;//You will get date object relative to server/client
timezone wherever it is parsed
try {
date = dateFormat.parse("2019-06-27T12:30:00.000+0000");
} catch (ParseException e) {
e.printStackTrace();
}
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); //If you need time just put specific format for time like 'HH:mm:ss'
String dateStr = formatter.format(date);
You're missing out on parts of the Date
The pattern you need is,
DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+'ssss");
You're missing the millisecond portion of your SimpleDateFormat, below is what you need for that specific pattern:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

Android - Getting different UTC time

I have two methods for get UTC date time.
method:1) get current UTC
public static String getUTCdatetimeAsString()
{
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd- HH:mm:ss", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
final String utcTime = sdf.format(new Date());
Log.e("utcTime : " ,""+ utcTime);
return utcTime;
}
which print below Log
utcTime : 2018-07-12- 12:37:09
method:2) select date from calendar and get UTC
public static String dateUTCToLocal() {
try {
SimpleDateFormat formatter = new SimpleDateFormat("d/M/yyyy HH:mm:ss", Locale.US);
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date value = formatter.parse("12/7/2018 06:07:09");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd- HH:mm:ss", Locale.US);
format.setTimeZone(TimeZone.getDefault());
return format.format(value);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
which print below Log
2018-07-12- 11:37:09
Problem : getting different UTC date (1 hour different )not understand why i tried many solutions but not getting perfect result, any help appreciate thanks in advance.
Try this format ..
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd- HH:mm:ss a", Locale.US);
it show time into am pm wise.
The line "format.setTimeZone(TimeZone.getDefault());" sets the timezone you are currently in while "sdf.setTimeZone(TimeZone.getTimeZone("UTC"));" sets the timezone to "UTC". You should stick with the second line if you want to get UTC-time

DateTime Local time zone to UTC and UTC to some other selected timezone conversion in Android

I am developing an android application. In my app I stored my logged-in local date time in preference and showing in the user profile page. After some of days I changed my mobile timezone as Australia. Now I need to convert my existing local date time to Australia timezone date time and display in profile page. Is it possible. Show me the code snippet. Your help will be appreciated. Thanks in advance.
String inputText = "2017-05-17 15:50:00";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.ENGLISH);
df.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
SimpleDateFormat outputFormat =
new SimpleDateFormat("MMM dd, yyyy h:mm a");
// Adjust locale and zone appropriately
Date date = null;
try {
date = df.parse(inputText);
} catch (ParseException e) {
e.printStackTrace();
}
String outputText = outputFormat.format(date);
System.out.println(outputText);
Try this code.. it may help

Android timezone issue, confused

I have a date like this Tue Jun 21 14:47:37 GMT+05:30 2016 , which I create myself. I create it using calendar. Where user selects a date and I save it as milliseconds from calendar.
When I send it , send it, I again create a calendar instance put the saved milliseconds into it and get the date like this :
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getDefault());
calendar.setTimeInMillis(SSPreferences.getDate());
Now while getting date from calendar i do this :
calendar.getTime()//This is what I send to server.
I send it to server, but when server sends me the date, it is always 5.5 hours before my time.
I know my time is GMT+5:50. So what server is doing on its side ?
How do I send the date , such that I get back the same date which I sent to the server.
Any help is appreciated.
Thanks
check this
static final String DATEFORMAT = "yyyy-MM-dd HH:mm:ss"
public static Date GetUTCdatetimeAsDate()
{
//note: doesn't check for null
return StringDateToDate(GetUTCdatetimeAsString());
}
public static String GetUTCdatetimeAsString()
{
final SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
final String utcTime = sdf.format(new Date());
return utcTime;
}
public static Date StringDateToDate(String StrDate)
{
Date dateToReturn = null;
SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);
try
{
dateToReturn = (Date)dateFormat.parse(StrDate);
}
catch (ParseException e)
{
e.printStackTrace();
}
return dateToReturn;
}
Finally I solved the issue by adding/subtracting the rawOffSet from the local time zone.
TimeZone timeZone = TimeZone.getDefault();
int offset = timeZone.getOffset(SSPreferences.getDate());
WriteLog.Print("offset is "+offset);
long newtime = SSPreferences.getDate() + offset;
calendar.setTimeInMillis(newtime);

Datetime conversion one timezone to another timezone in Android

I stuck with timezone conversion can any one help me to get out of it
My code for time conversion is below
public static String convertToLocalTimeZone(String date, String timeZone)
{
SimpleDateFormat df1 = new SimpleDateFormat("MM/dd/yyyy HH:mm a");
df1.setTimeZone(TimeZone.getTimeZone(timeZone));
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone(timeZone));
try {
calendar.setTime(df1.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat df2 = new SimpleDateFormat("MM/dd/yyyy HH:mm a");
df2.setTimeZone(TimeZone.getDefault());
calendar.setTimeZone(TimeZone.getDefault());
return df2.format(calendar.getTime());
}
Parameter timeZone is America/New_York and expected is Asia/Calcutta
I'm getting two different string for datetime and time zone now I have to convert datetime according to device local timezone
I have used Joda-Time and with some modification I have implemented and fixed my issue.

Categories

Resources