I have this:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date result = dateFormat.parse(this.getCreatedTime());
Basically I want to convert a string like "2016-09-27T09:19:57Z" into something like "September 27, 2016 at 9:19 AM".
If I use the code above I end up with a Date object, but all the methods are deprecated. So how do I achieve this?
You can use DateFormat again as #Thomas wrote:
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date inputDate = inputFormat.parse(this.getCreatedTime());
DateFormat outputFormat = new SimpleDateFormat("outputFormat");
String output = outputFormat.format(inputDate);
You should do research before posting any question here.
Use this to get Date from your required pattern
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
Date date = null;
try {
date = format.parse(unformattedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Make new instance of your desired patter.
format = new SimpleDateFormat("MMMM dd, yyyy 'at' HH:mm a", Locale.getDefault());
String formattedDate = format.format(date);
Related
I have this string: 2018-09-22 10:17:24.772000
I want to convert it to Date:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
String sdate = "2018-09-22 10:17:24.772000";
Date dateFrom = simpleDateFormat.parse(sdate);
but it shows: Sat Sep 22 10:17:24 GMT+03:30 2018
Here is what you should do instead, you are printing date object itself, you should print its format.
I will provide the code with old date api and new local date api :
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
String sdate = "2018-09-22 10:17:24.772000";
Date dateFrom = simpleDateFormat.parse(sdate);
System.out.println(dateFrom); // this is what you do
System.out.println(simpleDateFormat.format(dateFrom)); // this is what you should do
// below is from new java.time package
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
System.out.println(LocalDateTime.parse(sdate, formatter).format(formatter));
output is :
Sat Sep 22 10:30:16 EET 2018
2018-09-22 10:30:16.000000
2018-09-22 10:17:24.772000
Hope This will help you
public class Utils {
public static void main(String[] args) {
String mytime="2018-09-22 10:17:24.772000";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSSSSS");
Date myDate = null;
try {
myDate = dateFormat.parse(mytime);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");
String finalDate = timeFormat.format(myDate);
System.out.println(finalDate);
}
}
Looks to me like you have converted it to a Date. What is your desired result? I suspect what you are wanting to do is to create another Simple date format that shows your expected format and then use simpledateformat2.format(dateFrom)
I should also point out based on past experience that you should add a Locale to your simple date formats otherwise a device with a different language setting may not be able to execute this code
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS", Locale.US);
String startDateStr = "2017-02-03"
DateFormat formatter = new SimpleDateFormat("yyyy-MM-DD",Locale.US);
Date date = (Date)formatter.parse(startDateStr);
2017-02-03 date is parsed to Tue Jan 03 00:00:00 GMT+05:45 2017
Did I
miss something?
Update
I needed a string to be converted to a date object
while maintaining the same format.
The reason for this is I want to make use of public boolean after(Date when) method
This will work ^_^
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat outputFormat = new SimpleDateFormat("dd MMM yyyy");
String startDateStr ="2017-02-03";
Date date = null;
try {
date = inputFormat.parse(startDateStr);
String startDateStrNewFormat = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
Little explanation of your output :
D is Day in year (1-365)
d is day in month (1-31)
Check the document
Use SimpleDateFormat type for fomatter. You are creating DateFormat object but using SimpleDateFormat.
String startDateStr = "2017-02-03"
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
Date date = (Date)formatter.parse(startDateStr);
Yes you missed something. You used DD instead of dd in your yyyy-MM-DD format string. Here is how you do it:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(new Date());
I'm having a problem in parsing a date. I'm new on android and try to search for the solution but it seems no luck. I'd already tried to follow this one http://developer.android.com/reference/java/text/SimpleDateFormat.html but still got an error.
please help me..
I try to parse this date 2014-03-18T02:07:35.742-0400 and try to format to this 03/18/2014 02:07
I got this error:
java.lang.IllegalArgumentException
at java.text.DateFormat.format(DateFormat.java:361)
at java.text.Format.format(Format.java:93)
Try something like this:
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse("2014-03-18T02:07:35.742-0400");
System.out.println(new SimpleDateFormat("MM/dd/yyyy HH:mm").format(date));
For my time zone prints:
03/18/2014 10:07
private final DateFormat parsedFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm", Locale.getDefault());
private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault());
Date date = dateFormat.parse(dateFormat.format(your_date));
Date parsedDate = parsedFormat.parse(parsedFormat.format(date));
Reference
use following code
try{
String srcDate = new String("2014-03-18T02:07:35.742-0400");
SimpleDateFormat srcDf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
SimpleDateFormat destDf = new SimpleDateFormat("yyyy/MM/dd");
Date date = srcDf.parse(srcDate);
// format the date into another format
dateStr = destDf.format(date);
}catch (ParseException e) {
// TODO: handle exception
}
I looked around here and could not find anything that matches what I need.
I get the following String:
"March 13, 2013"
and need to transform it into
"2013-03-13"
I tried using this code:
SimpleDateFormat inputFormat = new SimpleDateFormat("MMMMMMMMMM dd, YYYY");
SimpleDateFormat outputFormat = new SimpleDateFormat("YYYY-MM-dd");
Date d = inputFormat.parse(startDate);
System.out.println(outputFormat.format(d));
but i get "203-12-30".. Why is that?
Y represents week year. Try using y
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
I just copied and pasted your code made a few small changes and got the result you were looking for. The main change I made was changing the upper case "Y" in inputFormat and outputFormat to lower case "y". Anyway here you are:
String startDate = "March 13, 2013";
SimpleDateFormat inputFormat = new SimpleDateFormat("MMMMMMMMMM dd, yyyy");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d = inputFormat.parse(startDate);
System.out.println(outputFormat.format(d));
} catch (ParseException e) {
e.printStackTrace();
}
Have a try with the following code:
String startDate = "March 13, 2013";
SimpleDateFormat inputFormat = new SimpleDateFormat("MMMM dd, yyyy",Locale.ENGLISH);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
Date d = inputFormat.parse(startDate);
System.out.println(outputFormat.format(d));
Hi I have the following date as String format.
Input
2010-04-20 05:34:58.0
Output I want the string like
20, Apr 2010
Can someone tell me how to do it ?
Try this:
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
DateFormat outputFormat = new SimpleDateFormat("dd, MMM yyyy");
Date yourDate = inputFormat.parse("2010-04-20 05:34:58.0");
String formattedDate = outputFormat.format(yourDate);
You might want to try this:
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date date = inFormat.parse( "2010-04-20 05:34:58.0");
SimpleDateFormat outFormat = new SimpleDateFormat("dd, MMM yyyy");
System.out.println(outFormat.format( date));