How to create a valid DateTime object from String - android

I have the following String.
21-Mar-2014
How can i convert this into a valid Joda DateTime object?
I've tried the following with no joy:
DateTimeFormatter formatter = DateTimeFormat.forPattern("d-MMM/Y");
DateTime dt = formatter.parseDateTime(date);
Thanks in advance
Matt

There are already so many question in SO related to this.
Check this.

A genuine joda answer with corrected pattern string and explicit Locale:
String input = "21-Mar-2014";
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd-MMM-yyyy").withLocale(Locale.ENGLISH);
DateTime dt = dtf.parseDateTime(input); // using the default time zone
System.out.println(dt); // 2014-03-21T00:00:00.000+01:00 (my zone: Europe/Berlin)
If you don't need time part (regarding your input!) then I recommend to use:
LocalDate date = dtf.parseLocalDate(input);

String dateInString = "7-Jun-2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}

you can try this
Date date1;
String myFormatString = "MMM-dd-yyyy"; // for example
String Your_Date=txtDate.getText().toString();
SimpleDateFormat df = new SimpleDateFormat(myFormatString);
date1 = df.parse(Your_Date);

you just use the following simple date format
SimpleDateFormat sdf=new SimpleDateFormat("d-MMM-yyyy");
Date date1=sdf.parse("21-Mar-2014");
Try this
DateTimeFormatter formatter = DateTimeFormat.forPattern("d-MMM-yyyy");
DateTime dt = formatter.parseDateTime(date);
System.out.println(dt.toString());

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 date picker set time

i would like to set the date format of my date picker like this:
yyyy-MM-dd HH:ss:mm
I hope this is the correct format for dates in databases. if not, please tell me which format is right.
also i would like to set the hour,minute and second of selected date like 0
Example:
2015-11-30 00:00:00
For this i use this code:
long dateTime = DatePicker.getCalendarView().getDate();
Date date = new Date(dateTime);
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
Android Studio tells me, that setHours,setMinutes and setSeconds is deprecated. what is the new way to set this values?
Use Calendar Object instead of Date:
Calendar c = Calendar.getInstance();
c.setTimeInMillis(DatePicker.getCalendarView().getDate(););
c.set(Calendar.HOUR,00);
like wise
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");
String dateString = "2015-11-30 00:00:00";
try {
Date date = df.parse(dateString);
df.applyPattern("yyyy-mm-dd hh:mm:ss");
String result = df.format(date);
}
catch (ParseException e){
e.printStackTrace();
}

private static final Date myDate =?

I was having difficult to put correct types for:
private static final Date myDate =
I tried to put "mm-dd-yyyy" but it said it cannot be a string.
I tried 'mm-dd-yyyy' but it said too many characters in character literal.
I dont know what is other types I should put. Help please.
If you want to have a Date with a specific time you can use a Calendar object.
If you want to have todays Date you can just use:
private static final Date myDate = new Date();
Specific date:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2015);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date myDate = cal.getTime();
That would be 01.01.2015
Try with this:
SimpleDateFormat f = new SimpleDateFormat("mm-dd-yyyy");
Date date = f.parse(yourString);
I expect it will works for you!
EDIT: It gives to you an error because you are trying set to your variable myDate a String, and it's a type of Date.
Try something like:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "7-Jun-2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
...as you can see here:
http://www.mkyong.com/java/how-to-convert-string-to-date-java/

how to format 2013-05-15T10:00:00-07:00 to Date android

I am trying to format the date string to Date, and then get the month/day from it:
String strDate="2013-05-15T10:00:00-07:00";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss-z");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat sdfmonth = new SimpleDateFormat("MM/dd");
String monthday= sdfmonth.format(convertedDate);
but it returns me current month/day i.e 5/18. Whats wrong?
3 things :
There is a mistake in your format : 2013-05-15T10:00:00-07:00 has no meaning, it should be 2013-05-15T10:00:00-0700 (with no colon at the end, this is a timezone defined in RFC 822. (Look at the docs about Z).
Change your format to yyyy-MM-dd'T'HH:mm:ssZ as mentionned by #blackbelt
you get a bad date because you re-format your date whatever happens during parsing. Re-format in your try block, if and only if parsing worked.
----------Update
String strDate = "2013-05-15T10:00:00-0700";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(strDate);
SimpleDateFormat sdfmonth = new SimpleDateFormat("MM/dd");
String monthday = sdfmonth.format(convertedDate);
} catch (ParseException e) {
e.printStackTrace();
}
I don't know what is wrong in your code. for me it throws Unparseable exception like this.
java.text.ParseException: Unparseable date: "2013-05-15T10:00:00-07:00"
But the following way works well.
String strDate="January 2, 2010";
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM d, yyyy");
Date date = dateFormat.parse(strDate);
System.out.println(date.getMonth());
But in java Date is deprecated as per http://docs.oracle.com/ . Try to use Calender instead of Date.
I hope this will help you.

Convert DateString in a particular format

I have a problem that I have Date String in the format "2011-09-28T10:33:53.694+0000". I want to change this Date format in MM/DD/YYYY, don't know How? Please suggest me for right result.
Something like this. Check the details for your "from" format.
DateFormat from = new SimpleDateFormat("yyyy-MM-ddThh:mm:ss"); // not sure about the details
from.setLenient(false);
DateFormat to = new SimpleDateFormat("MM/dd/yyyy");
to.setLenient(false);
Date d = from.parse(dateStr);
System.out.println(to.format(d));
String string = "2011-09-28T10:33:53.694+0000";
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH);
SimpleDateFormat outFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date date;
String output;
try {
date = inFormat.parse(string);
output = outFormat.format(date); // 28/09/2011
} catch (ParseException e) {
e.printStackTrace();
}
Do it this way ---->
Create object Date class
Date date = new Date();
Create object of Simple Date Format class
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Note -> MM - will give month in character and dd and yyyy will give date and year in numeric
Convert date into string
String s = sdf.format(date);
and you are done with it...

Categories

Resources