java - Date from datepicker flips day and month -
when select date datepicker day , month flipping positions.i want mm/dd/yyyy. instead getting dd/mm/yyyy.
i tried few methods described on site fix didn't help. such hour_of_day, instead of hour. however, doesn't seem work.
any advice, suggestions?
thanks in advance :)
import android.app.activity; import android.app.datepickerdialog; import android.app.dialog; import android.app.progressdialog; import android.content.context; import android.content.intent; import android.net.connectivitymanager; import android.net.networkinfo; import android.os.asynctask; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.datepicker; import android.widget.edittext; import android.widget.textview; import android.widget.toast; import sync.databasehandler; import sync.userfunctions; import org.json.jsonexception; import org.json.jsonobject; import java.io.ioexception; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.util.calendar; public class register extends activity { /** * json response node names. **/ private static string key_success = "success"; private static string key_uid = "uid"; private static string key_firstname = "fname"; private static string key_lastname = "lname"; private static string key_dob = "dob"; private static string key_email = "email"; private static string key_created_at = "created_at"; private static string key_error = "error"; /** * defining layout items. **/ edittext inputfirstname; edittext inputlastname; edittext inputemail; edittext inputpassword; button btnregister; textview registererrormsg; //datpicker setup button datebutton; static final int date_dialog_id = 999; private datepicker dobpicker; textview dobresultview; private int year; private int month; private int day; string dob; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.register); inputfirstname = (edittext) findviewbyid(r.id.firstnameinput); inputlastname = (edittext) findviewbyid(r.id.lastnameinput); inputemail = (edittext) findviewbyid(r.id.emailinput); inputpassword = (edittext) findviewbyid(r.id.passwordinput); btnregister = (button) findviewbyid(r.id.registerbtn); registererrormsg = (textview) findviewbyid(r.id.register_error); dobpicker = (datepicker)findviewbyid(r.id.dobselect); datebutton = (button)findviewbyid(r.id.datebtn); dobresultview = (textview)findviewbyid(r.id.dobresultview); setcurrentdateonview(); /** * return login screen **/ button login = (button) findviewbyid(r.id.loginbtn); login.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { intent myintent = new intent(view.getcontext(), login.class); startactivityforresult(myintent, 0); finish(); } }); /** * register button click event. * toast on fields if empty. **/ datebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { showdialog(date_dialog_id); dob = " ("+integer.tostring(day)+"/"+integer.tostring(month+1)+"/"+integer.tostring(year)+")"; dobresultview.settext(dob); } }); btnregister.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { if ( ( !inputpassword.gettext().tostring().equals("")) && ( !inputfirstname.gettext().tostring().equals("")) && ( !inputlastname.gettext().tostring().equals("")) && ( !inputemail.gettext().tostring().equals("")) ) { netasync(view); } else { toast.maketext(getapplicationcontext(), "one or more fields empty", toast.length_short).show(); } } }); } /** * async task check internet connection working **/ private class netcheck extends asynctask<string,string,boolean> { private progressdialog ndialog; @override protected void onpreexecute(){ super.onpreexecute(); ndialog = new progressdialog(register.this); ndialog.setmessage("loading.."); ndialog.settitle("checking network"); ndialog.setindeterminate(false); ndialog.setcancelable(true); ndialog.show(); } @override protected boolean doinbackground(string... args){ /** * gets current device state , checks working internet connection trying google. **/ connectivitymanager cm = (connectivitymanager) getsystemservice(context.connectivity_service); networkinfo netinfo = cm.getactivenetworkinfo(); if (netinfo != null && netinfo.isconnected()) { try { url url = new url("http://www.google.com"); httpurlconnection urlc = (httpurlconnection) url.openconnection(); urlc.setconnecttimeout(3000); urlc.connect(); if (urlc.getresponsecode() == 200) { return true; } } catch (malformedurlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } return false; } @override protected void onpostexecute(boolean th){ if(th == true){ ndialog.dismiss(); new processregister().execute(); } else{ ndialog.dismiss(); registererrormsg.settext("error in network connection"); } } } private class processregister extends asynctask<string, string, jsonobject> { /** * defining process dialog **/ private progressdialog pdialog; string email,password,fname,lname,dob; @override protected void onpreexecute() { super.onpreexecute(); inputpassword = (edittext) findviewbyid(r.id.passwordinput); fname = inputfirstname.gettext().tostring(); lname = inputlastname.gettext().tostring(); email = inputemail.gettext().tostring(); dobpicker = (datepicker)findviewbyid(r.id.dobselect); dobresultview = (textview)findviewbyid(r.id.dobresultview); dob = integer.tostring(day)+"/"+integer.tostring(month+1)+"/"+integer.tostring(year); password = inputpassword.gettext().tostring(); pdialog = new progressdialog(register.this); pdialog.settitle("contacting servers"); pdialog.setmessage("registering ..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } @override protected jsonobject doinbackground(string... args) { userfunctions userfunction = new userfunctions(); jsonobject json = userfunction.registeruser(fname, lname, email, dob, password); return json; } @override protected void onpostexecute(jsonobject json) { /** * checks success message. **/ try { if (json.getstring(key_success) != null) { registererrormsg.settext(""); string res = json.getstring(key_success); string red = json.getstring(key_error); if(integer.parseint(res) == 1){ pdialog.settitle("getting data"); pdialog.setmessage("loading info"); registererrormsg.settext("successfully registered"); databasehandler db = new databasehandler(getapplicationcontext()); jsonobject json_user = json.getjsonobject("user"); /** * removes previous data in sqlite database **/ userfunctions logout = new userfunctions(); logout.logoutuser(getapplicationcontext()); db.adduser(json_user.getstring(key_firstname),json_user.getstring(key_lastname),json_user.getstring(key_email),json_user.getstring(key_dob),json_user.getstring(key_uid),json_user.getstring(key_created_at)); /** * stores registered data in sqlite database * launch settings screen complete setup **/ intent registered = new intent(getapplicationcontext(), alarmactivity.class); /** * close views before launching registered screen **/ registered.addflags(intent.flag_activity_clear_top); pdialog.dismiss(); startactivity(registered); finish(); } else if (integer.parseint(red) ==2){ pdialog.dismiss(); registererrormsg.settext("user exists"); } else if (integer.parseint(red) ==3){ pdialog.dismiss(); registererrormsg.settext("invalid email id"); } } else{ pdialog.dismiss(); registererrormsg.settext("error occured in registration"); } } catch (jsonexception e) { e.printstacktrace(); } } } public void netasync(view view){ new netcheck().execute(); } // display current date public void setcurrentdateonview() { dobpicker = (datepicker) findviewbyid(r.id.dobselect); final calendar c = calendar.getinstance(); year = c.get(calendar.year); month = c.get(calendar.month); day = c.get(calendar.day_of_month); // set current date datepicker dobpicker.init(year, month, day, null); } @override protected dialog oncreatedialog(int id) { switch (id) { case date_dialog_id: // set date picker current date return new datepickerdialog(this, datepickerlistener, year, month,day); } return null; } private datepickerdialog.ondatesetlistener datepickerlistener = new datepickerdialog.ondatesetlistener() { // when dialog box closed, below method called. public void ondateset(datepicker view, int selectedyear, int selectedmonth, int selectedday) { year = selectedyear; month = selectedmonth; day = selectedday; dobpicker.init(year, month, day, null); dob = " ("+integer.tostring(day)+"/"+integer.tostring(month+1)+"/"+integer.tostring(year)+")"; dobresultview.settext(dob); } }; }
code view: regiser.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#6ec782" android:padding="10dp" android:orientation="vertical"> <textview android:id="@+id/registerinstructview" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_alignleft="@+id/registerbtn" android:layout_alignparenttop="true" android:layout_margintop="33dp" android:text="@string/registerinstructtxt" android:textcolor="#ffffff" /> <edittext android:id="@+id/emailinput" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/passwordinput" android:layout_below="@+id/registerinstructview" android:layout_margintop="23dp" android:ems="10" android:inputtype="textemailaddress" android:textcolor="#ffffff" > <requestfocus /> </edittext> <edittext android:id="@+id/firstnameinput" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/passwordinput" android:layout_below="@+id/passwordinput" android:ems="10" android:inputtype="textpersonname" android:textcolor="#ffffff" /> <edittext android:id="@+id/passwordinput" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/emailinput" android:layout_centerhorizontal="true" android:ems="10" android:inputtype="textpassword" android:textcolor="#ffffff" /> <edittext android:id="@+id/lastnameinput" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/firstnameinput" android:layout_below="@+id/firstnameinput" android:ems="10" android:inputtype="textpersonname" android:textcolor="#ffffff" /> <textview android:id="@+id/lastnameview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/lastnameinput" android:layout_toleftof="@+id/lastnameinput" android:text="@string/lastnametxt" android:textcolor="#ffffff" /> <textview android:id="@+id/firstnameview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/lastnameinput" android:layout_toleftof="@+id/firstnameinput" android:text="@string/firstnametxt" android:textcolor="#ffffff" /> <textview android:id="@+id/register_error" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/registerbtn" android:layout_centerhorizontal="true" android:layout_margintop="37dp" android:textcolor="#e30000" android:padding="10dip" android:textstyle="bold"/> <textview android:id="@+id/passwordtxtview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/firstnameinput" android:layout_alignright="@+id/firstnameview" android:text="@string/passwordtxt" android:textcolor="#ffffff" /> <textview android:id="@+id/emailtxtview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/emailinput" android:layout_alignright="@+id/passwordtxtview" android:text="@string/emailtxt" android:textcolor="#ffffff" /> <textview android:id="@+id/dobresultview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/dobselect" android:layout_centerhorizontal="true" android:layout_margintop="25dp" android:textcolor="#ffffff" /> <datepicker android:id="@+id/dobselect" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/datebtn" android:layout_centerhorizontal="true" android:layout_margintop="23dp" android:calendarviewshown="false" /> <button android:id="@+id/registerbtn" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_alignleft="@+id/datebtn" android:layout_below="@+id/dobresultview" android:layout_margintop="63dp" android:text="@string/registerbtntxt" android:textcolor="#ffffff" /> <button android:id="@+id/datebtn" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_below="@+id/lastnameinput" android:layout_centerhorizontal="true" android:layout_margintop="38dp" android:text="@string/datebtntxt" android:textcolor="#ffffff" /> <button android:id="@+id/loginbtn" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/register_error" android:layout_alignbottom="@+id/register_error" android:layout_alignleft="@+id/registerbtn" android:text="@string/loginbtntxt" android:textcolor="#ffffff" />
your using dob = " ("+integer.tostring(day)+"/"+integer.tostring(month+1)+"/"+integer.tostring(year)+")";
instead of use ("+integer.tostring(month+1)+"/"+integer.tostring(day)+"/"+integer.tostring(year)+")";
Comments
Post a Comment