I have a SimpleDateFormat like this :
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
and trying to parse such 2012-Jul-29 17:14:39
but I`m getting
java.text.ParseException: Unparseable date: "2012-Jul-29 17:14:39" at
java.text.DateFormat.parse(Unknown Source) at
com.sysplan.visixd.blastgauge.BGParser.main(Parser.java:396)
Any idea why ?
It appears to be a locale problem, I tried this without any error
new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss").parse("2012-Jul-29 17:14:39");
However this failed:
new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss", Locale.TAIWAN)
.parse("2012-Jul-29 17:14:39");
So it appears to be a locale problem, you need to specify your locale to ENGLISH
new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss", Locale.ENGLISH)
.parse("2012-Jul-29 17:14:39");
That is:
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
"yyyy-MMM-dd HH:mm:ss", Locale.ENGLISH);
Try this
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss", Locale.ENGLISH);
try this
new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss", Locale.ENGLISH);
You can use next code:
import java.text.DateFormat;
import java.util.Calendar;
public static void main(String[] args) {
// TODO Auto-generated method stub
Calendar now = Calendar.getInstance();
DateFormat formateadorFechaMedia = DateFormat.getDateInstance(DateFormat.MEDIUM);
System.out.println(formateadorFechaMedia.format(now.getTime()));
}
}
Related
I am trying to format the following input date: "2019-02-12 18:00:40""
to the following format "dd-MM-yyyy". However, I am experiencing mixed results with the date formatter method I created below and the output is as follows
"Wed Aug 11 00:00:00 GMT+02:00 17"
private String formatDate(String dateT) throws ParseException
{
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date date = formatter.parse(dateT);
return date.toString();
}
As mentioned, you'll need two formats to get your desired result.
If you can use Java8+, I suggest using LocalDateTime and DateTimeFormatter (instead of SimpleDateFormat):
String stamp = "2019-02-12 18:00:40";
LocalDateTime ldt = LocalDateTime.parse(stamp, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(ldt.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")));
Output:
12-02-2019
Edit:
If you really must use the outdated classes, you can apply the same principle with SimpleDateFormat:
String stamp = "2019-02-12 18:00:40";
SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dt1.parse(stamp);
SimpleDateFormat dt2 = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(dt2.format(date));
As suggested by #Robert. This was the solution I ended up using with two simpledateformatters.
private String formatDate(String date) throws ParseException {
SimpleDateFormat inputDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currentDate = inputDate.parse(date);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String formattedDate = formatter.format(currentDate);
return formattedDate;
}
My Date constructor is deprecated and highlighted in Yellow.
How can I use Calendar.Set() to resolve this issue. I have called both import java.util.Calendar; and date.
Code is below. Thanks in advance.
Format f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
Date d = new Date(f.format(geoState.getString("fireTime")));
temp.setFireTime(d);
Use a DateFormat.parse method to convert a String to Date
String string = "January 2, 2010";
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);
In your case it will be something like this
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
Date date = format.parse(geoState.getString("fireTime"));
temp.setFireTime(date);
I have the same issue while migrating my project to Java 11. Here is the answer
new Date("08/10/2020");
to
DateFormat.getDateInstance().parse("08/10/2020")
I have the following code:
String s = "08-12-2014 05:00:00"
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
SimpleDateFormat outputFormat = new SimpleDateFormat("EEEE, HH:mm a");
Date oneWayTripDate = null;
try {
oneWayTripDate = inputFormat.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
String datetime = outputFormat.format(oneWayTripDate);
but for some weird reason it always returns the wrong day of the week. what am i doing wrong?
The input SimpleDateFormat pattern is wrong. Given the date 08-12-2014 05:00:00 with the year part at the end, and assuming 08 is the month, the format should be:
SimpleDateFormat inputFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss", Locale.ENGLISH);
See the Javadoc of SimpleDateFormat for how to define date patterns.
I am trying to use simple date format to format the current time:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Date now = new Date(System.currentTimeMillis());
try {
java.util.Date formattedDate = sdf.parse(now.toString());
System.out.println(formattedDate.toString());
} catch (ParseException e) {
e.printStackTrace();
}
However the output I am getting is:
Fri Oct 18 00:00:00 CDT 2013
I am trying to achieve:
2013/08/18
What am I doing wrong?
You have to use the SimpleDateFormat#format(Date date) method, which returns the date formatted in the desired way. Note that this method is inherited from DateFormat.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Date now = new Date(System.currentTimeMillis());
String formattedDate = sdf.format(now);
System.out.println(formattedDate);
You are parsing your Date but not formatting it with your SimpleDateFormat. The Date#toString() method has its own output format. Use your SimpleDateFormat instead.
System.out.println(sdf.format(formattedDate));
You don't need to call parse
You don't need try/catch
Try this much simpler code:
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String formattedDate = sdf.format(new Date());
System.out.println(formattedDate);
You can try using this
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
public class DateFormatTest {
public static void main(String[] args) {
Date now = new Date();
// Use Date.toString()
System.out.println(now);
// Use DateFormat
DateFormat formatter = DateFormat.getInstance(); // Date and time
String dateStr = formatter.format(now);
System.out.println(dateStr);
formatter = DateFormat.getTimeInstance(); // time only
System.out.println(formatter.format(now));
// Use locale
formatter = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.FRANCE);
System.out.println(formatter.format(now));
// Use SimpleDateFormat
SimpleDateFormat simpleFormatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println(simpleFormatter.format(now));
}
}
In android
am getting date in
(date = "04-01-2013") this format
but i want to show same date in
en.US format like (date="Friday,January 04,2013")
use SimpleDateFormat.
set input pattern matching input date string: "04-01-2013" -> "dd-MM-yyyy".
And output pattern like output: "Friday, January 04, 2013" -> "EEEE, MMMM dd, yyyy"
public String formatDate(String input){
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date d = sdf.parse(input);
sdf.applyPattern("EEEE, MMMM dd, yyyy");
return sdf.format(d,new StringBuffer(),0).toString();
}
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;
}
You can use something like this
android.text.format.DateFormat.format("EEEE, MMMM dd, yyyy", new java.util.Date());
Take a look at DateFormat.
This is how you do it in java
SimpleDateFormat df=new SimpleDateFormat("EEEE,MMMM dd,yyyy");
java.util.Date date=df.parse("04-01-2013");
refer this