2

I have the Date and Time like this 2016/11/28 and time 07:30 PM. And combine this string and make like below

string mydate = extras.GetString("Apodate") + " " + extras.GetString("Apostarttime");

so mydate string contain 2016/11/28 07:30PM.

No I want to convert this string to below format

"yyyy-MM-dd'T'HH:mm:ss'Z'"

So I try this way:

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
try
{
    Java.Util.Date startdate = dateFormat.Parse(mydate);
    Java.Util.Date enddate = dateFormat.Parse(mydate1);
    SimpleDateFormat rformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    DateTime dt1 = DateTime.ParseExact(rformat.Format(startdate).ToString(), "yyyy-MM-dd'T'HH:mm:ss'Z'", CultureInfo.InvariantCulture);
    model.StartTime = dt1;
}
catch (ParseException e)
{
    e.PrintStackTrace();
}

But my model.StartTime contain 12/11/0195 7:30:00 PM. But I want

2016/11/28 7:30:00 PM. as a DateTime.

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95

1 Answers1

0

First the thing you should consider to do is
1. Convert String(type) To DateTime(type)
- Search how parseExact do first, I think "yyyy-MM-dd'T'HH:mm:ss'Z'" is not the correct datetime format
2. When you got Datetime(type) you can display to any string format you want
- Search how to convert type DateTime to string

Link
https://stackoverflow.com/questions/5366285/parse-string-to-datetime-in-c-sharp#=
How do I get the AM/PM value from a DateTime?

TLDR;

 SimpleDateFormat rformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");//Format text as what ParseExact method can do 
 DateTime dt1 = DateTime.ParseExact(rformat.Format(startdate).ToString(), "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);//plz become Datetime!!! And don't give me a runtime error here
 model.StartTime = dt1;

And when you want to display model.StartTime

 string Datetxt = model.StartTime.ToString("yyyy/MM/dd'T'HH:mm tt'Z'", CultureInfo.InstalledUICulture) //now become a beautiful string with unwanted chars 'T' and  'Z'

Result

user3682728
  • 467
  • 3
  • 7