Reputation: 404
i have done of getting and set date and time to my TextView, now i want to know "How to convert Date and time to milliSeconds when i select and set from DatePicker and TimePicker to TextView", help me with codes
my tried code,
public class MainActivity extends AppCompatActivity {
private TextView txtDateSet;
private TextView txtTimeSet;
int cDay, cMonth, cYear;
int tHours, tMinutes;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
txtDateSet = (TextView) findViewById (R.id.txt_date_display);
txtTimeSet = (TextView) findViewById (R.id.txt_time_display);
txtDateSet.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View v) {
displayAlertDialog ();
}
});
}
public void displayAlertDialog () {
AlertDialog.Builder builder = new AlertDialog.Builder (this);
LayoutInflater inflater = (LayoutInflater) getSystemService (Context.LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate (R.layout.custom_alert, (ViewGroup) findViewById (R.id.lnt_root));
final DatePicker dateSet = (DatePicker) layout.findViewById (R.id.date_picker);
final TimePicker timeSet = (TimePicker) layout.findViewById (R.id.time_picker);
final TextView txtOk = (TextView) layout.findViewById (R.id.txt_ok);
builder.setView (layout);
final AlertDialog alertDialog = builder.create ();
alertDialog.setCanceledOnTouchOutside (true);
alertDialog.show ();
txtOk.setOnClickListener (new View.OnClickListener () {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onClick (View v) {
cDay = dateSet.getDayOfMonth ();
cMonth = dateSet.getMonth ();
cYear = dateSet.getYear ();
tHours = timeSet.getCurrentHour ();
tMinutes = timeSet.getCurrentMinute ();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, cYear);
cal.set(Calendar.MONTH, cMonth);
cal.set (Calendar.DAY_OF_MONTH, cDay);
cal.set (Calendar.HOUR_OF_DAY, tHours);
cal.set (Calendar.MINUTE, tMinutes);
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy");
String formatedDate = sdf.format(cal.getTime ());
txtDateSet.setText (""+formatedDate);
SimpleDateFormat stf = new SimpleDateFormat ("hh:mm aa");
String formatedTime = stf.format (cal.getTime ());
txtTimeSet.setText (""+formatedTime);
alertDialog.dismiss ();
}
});
}}
Upvotes: 0
Views: 750
Reputation: 1277
you can Try follow code for getting miliseconds from Date.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = format.parse(text);
long millis = date.getTime();
and we also having one Standard Library Called JodaTime Which is Provide Lot's off functinality related with date time.using this library you can easily get Miliseconds from DateTime. I am Also Having one Code Snippet Which is just return Miliseconds from DateTime.
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")
.withLocale(Locale.US)
.withZoneUTC();
DateTime datetime = formatter.parseDateTime(text);
long millis = dateTime.getMillis();
//Or
//long millis = formatter.parseMillis(text);
I hope you are Clear with My Idea.
Best of Luck
Upvotes: 1
Reputation: 3353
Do it like this,
public String getDay(String str){
if(str.equalsIgnoreCase("mon")
return "Monday";
....
}
as per your code
String mDate = dayFormat.format (cDay);
StringBuilder dateBuilder = new StringBuilder ()
.append (getDay(mDate)).append (",")
.append (mMonth).append (" ")
.append (cDay).append (",")
.append (cYear);
OR
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMM dd, YYYY");
Upvotes: 0