String date to milliseconds - android

I have a date String (format: dd-mmm-yyyy) and I would like to convert it into milliseconds, but it has to have the time zone of the device.
I've already tried this, but unfortunetly the "f.parse("17-July-2018");" is not working right.
SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH); //Sample "17-July-2018"
try {
Date d = f.parse("17-July-2018");
bonusTime = d.getTime();
} catch (ParseException e) {
e.printStackTrace();
}

The parsing format for full month names is MMMM
SimpleDateFormat f = new SimpleDateFormat("dd-MMMM-yyyy", Locale.ENGLISH);
should work. MMM is for short names like "Jan" or "Feb".

Related

Get date from server api and convert its format?

I am currently parsing date from PHP server api and the date from api is "1970-04-22" now I need to display this date as "04-22-1970". I tried following code but its not display the exact format.
String date = jsonObject.optString("date"); // output is "1970-04-22"
SimpleDateFormat spf=new SimpleDateFormat("MMM, dd,yyyy hh:mm:ss aaa");
Date newDate=spf.parse(date);
spf= new SimpleDateFormat("MMM dd yyyy");
date = spf.format(newDate);
Log.e("date", String.valueOf(date));
String tempDate = "1970-04-22";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
Date date1=null;
try {
date1 = simpleDateFormat.parse(tempDate);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat newformat = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
String dateInReqFormat = newformat.format(date1);
In these cases you have to first convert the existing format to a date and then format the date to the string as you want it .

Unparseable date: "2017-01-02T01:41:24Z" (at offset 10)

2017-01-02T01:41:24Z is my actual date format and I want to convert this date in yyyy-MM-dd hh:mma format.
Please see the following code I tried so far,
String newsDate = "2017-01-02T01:41:24Z";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mma");
Date date = null;
try
{
date = sdf.parse(newsDate);
}
catch (ParseException e)
{
e.printStackTrace();
}
but at date = sdf.parse(newsDate); line I'm getting the following error:
"Unparseable date: "2017-01-02T01:41:24Z" (at offset 10)".
Please guide me, where could i have gone wrong?
Because you are using different Date Format which is not correct.
Change this
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mma");
to this
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Try this:
String newsDate = "2017-01-02T01:41:24Z";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mma");
try {
texview.setText(sdf1.format(sdf.parse(newsDate)));
} catch (ParseException e) {
e.printStackTrace();
System.out.println("bad pattern");
}
This works perfectly for me.
String dateNew= "2017-01-02T01:41:24Z";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mma");
try {
texview.setText(sdf1.format(sdf.parse(dateNew)));
} catch (ParseException e) {
e.printStackTrace();
System.out.println("bad pattern");
}
The other answers are wrong with respect to time zone / offset - interpretation. The trailing "Z" denotes UTC+00:00 and must not be interpreted just as literal. The documentation of Android defines the suitable pattern symbol X which can handle this kind of input. If you don't care about this detail then you will not get an exception but wrong data (which is even worse).
So the final solution including the fix for the hour-part looks like:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Note: Use XX instead of XXX if you have offsets without colon.

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.

Android Date format conversion 71

I have a date in 02/21/2013 04:52:10 PM this format.
How do I convert it into MM-dd-yyyy HH:mm. I already tried few things but it keeps throwing error
java.text.ParseException: Unparseable date: "02/21/2013 04:52:10 PM" (at offset 2)
I really need a help from date format expert.
Thanks
Try this one:
DateFormat inputFormat = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a", Locale.getDefault());
DateFormat outputFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm", Locale.getDefault());
try {
System.out.println("Converted: " + outputFormat.format(inputFormat.parse("02/21/2013 04:52:10 PM")));
} catch (ParseException e) {
e.printStackTrace();
}
Output: 01-21-2013 16:52
Checkout http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html for more
Cheers
Construct two SimpleDateFormat objects. The first you parse() the value from into a Date object, the second you use to turn the Date object back into a string, e.g.
try {
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
DateFormat df2 = new SimpleDateFormat("dd-MMM-yyyy");
return df2.format(df1.parse(input));
}
catch (ParseException e) {
return null;
}
Parsing can throw a ParseException so you would need to catch and handle that.
In addition, check this out:-
http://developer.android.com/reference/java/text/SimpleDateFormat.html
and more:-
http://msdn.microsoft.com/en-us/library/aa226054(SQL.80).aspx
and more:-
Date format conversion Android
You need verify if your String have / or - in the mask, case have / then you must used:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", LOCALE_BR);
Date convert = sdf.parse(data);
case your String have - then you must used:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", LOCALE_BR);
Date convert = sdf.parse(data);
Or then, use the replace() for change of the caracter - for /

String to Date Conversion Problem

I am trying to convert the following string in to date, I am able to convert the string into to date object successfully,But the Problem is in this string I want to convert the time in to am/pm i.e. 12 hr format, I tried different ways but unable to get the solution.
How to get the 12hr format time from this string ?
Here is my code:
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy"); //please notice the capital M
Date date;
try {
date = formatter.parse("Fri Jul 01 10:00:00 CDT 2011");
Log.e("ThankYou Block", ""+date.toString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TRY
public static String StringToDate(String dateToParse) {
Date formatter = new Date(HttpDateParser.parse(dateToParse));
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSS");
int offset = TimeZone.getDefault().getRawOffset();
formatter.setTime(formatter.getTime() + offset);
String strCustomDateTime = dateFormat.format(formatter);
return strCustomDateTime;
}

Categories

Resources