smophos
smophos

Reputation: 343

Formatting date from a DatePicker

I know I can get the date from a datepicker using the following methods:

I am also getting the time from a timepicker using the following methods:

I need to format this data into "yyyy-MM-dd HH:mm:ss" format to place it into an sqlite database as a DATETIME variable. Is there a method which can handle this?

I think I will have to build it manually like so:

if(month < 10){
    month = "0" + month;
}
if(day < 10){
    day  = "0" + day ;
}

The problem was that the months and days were single digit.

Upvotes: 0

Views: 699

Answers (3)

Usman Kurd
Usman Kurd

Reputation: 7023

You can get Current Date by this following and save it to the string

String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());

now pass this string to the Simpledateformat class and define your desired Format like following

 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String currentDateandTime = sdf.format(mydate);

Upvotes: 0

KDeogharkar
KDeogharkar

Reputation: 10959

SimpleDateFormat outputFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");

Date ModifiedDate  = outputFormat.parse(strdate);

Upvotes: 1

Avadhani Y
Avadhani Y

Reputation: 7626

Simple as below:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
dateFormat.parse(yourDateHere);

Upvotes: 0

Related Questions