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));
}
}
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;
}
I am trying to convert date from one format to another, the date entered is in this format : 'mm-dd-yyyy' to 'yyyy-mm-dd'.
I received the date from webpage in 'mm-dd-yyyy' format and when I insert this date in mysql using hibernate, the date changes to some anonymous value.
Please help !!!
{
import java.io.*;
import java.text.*;
import java.util.*;
class test{
public static void main(String...s)throws Exception{
Date date ;
String datestr;
DateFormat dateFormat1 = new SimpleDateFormat("mm-dd-yyyy");
DateFormat dateFormat2 = new SimpleDateFormat("yyyy-mm-dd");
date = dateFormat1.parse("01-01-2015");
datestr = dateFormat1.format(date);
System.out.println(date);
System.out.println(datestr);
date = dateFormat2.parse(datestr);
datestr = dateFormat2.format(date);
System.out.println(date);
System.out.println(datestr);
}
}
You can try like this;
DateFormat originalFormat = new SimpleDateFormat("MM-dd-yyyy", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = originalFormat.parse("01-21-2013");
String formattedDate = targetFormat.format(date);
System.out.println(formattedDate);
For Date type result;
DateFormat originalFormat = new SimpleDateFormat("mm-dd-yyyy", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("yyyy-mm-dd");
Date date = originalFormat.parse("01-21-2013");
String formattedDate = targetFormat.format(date);
java.util.Date dtt = targetFormat.parse(formattedDate);
java.sql.Date ds = new java.sql.Date(dtt.getTime());
System.out.println(ds);
System.out.println(dtt);
System.out.println(formattedDate);
End the output is;
2013-01-21
Mon Jan 21 00:01:00 EET 2013
2013-01-21
you can use this below code snippet
public static String formatDate (String date, String initDateFormat, String endDateFormat) throws ParseException {
Date initDate = new SimpleDateFormat(initDateFormat).parse(date);
SimpleDateFormat formatter = new SimpleDateFormat(endDateFormat);
String parsedDate = formatter.format(initDate);
return parsedDate;
}
Date initDate = new SimpleDateFormat("yyyy-MM-dd").parse("2015-03-05");
System.out.println("initDate == "+initDate);
output ==>> initDate == Thu Mar 05 00:00:00 ICT 2015
In HQL simple SQL function work :
DATE_FORMAT(DATE,'%d-%m-%Y')
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()));
}
}
I am using jdk- 1.6.
I am try to parse String "24-10-2012" date to Date (24-10-2012) but i am getting this error:
java.text.ParseException: Unparseable date: "18-11-2012"
java.text.DateFormat.parse(DateFormat.java:354)
I am parsing like this:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
Date date = formatter.parse(currentDate);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
System.out.println(formatter.parse(currentDate));
prints
Wed Oct 24 00:00:00 CEST 2012
Your problem cannot be reproduced with the code you have posted.
My hypothesis: your exception is thrown from a piece of code other than the one you are accusing of the error. You could try carefully analyzing the stack trace in order to track down the real culprit.
Date in java does not hold any format. Read more...
When I run
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
Date date = formatter.parse(currentDate);
System.out.println(date);
System.out.println(formatter.format(date));
I get
Wed Oct 24 00:00:00 BST 2012
24-10-2012
which is as I expected. Can you clarity what the problem is?
You can use this for the format "dd-mm-yyyy"
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
Date date = formatter.parse(currentDate);
System.out.println(formatter.format(date));
import java.util.Date;
import java.text.SimpleDateFormat;
public class SimpleFormatDate
{
public static void main(String args[]){
Date todaysDate = new java.util.Date();
// Formatting date into yyyy-MM-dd HH:mm:ss e.g 2008-10-10 11:21:10
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
// Formatting date into yyyy-MM-dd e.g 2008-10-10
formatter = new SimpleDateFormat("yyyy-MM-dd");
formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
// Formatting date into MM/dd/yyyy e.g 10/10/2008
formatter = new SimpleDateFormat("MM/dd/yyyy");
formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
}
}
output
Formatted date is ==>2008-10-10 13:03:54
Formatted date is ==>2008-10-10
Formatted date is ==>10/10/2008
Wait a second.. Why u need to parsing that if u have a right value ?
Anyway, i use this :
SimpleDateFormat oFormat = new SimpleDateFormat("yyyy-MM-dd");
String sDate = oFormat.format("24-10-2012");
it will appearing date like 2012-10-24. So if u want to parsing to dd-MM-yyyy, u just need change the format to what u want.
NB : Sorry if my english is bad. :D
I am getting date format as "YYYY-mm-dd hh:mm" as formatter object.
How can I format the input formatter object to get only "YYYY-mm-dd";?
I am getting date format as
"YYYY-mm-dd hh:mm" as formatter
object. How can i format the input
formatter object to get only
"YYYY-mm-dd";
You can not have date as YYYY-mm-dd it should be yyyy-MM-dd. To get date in yyyy-MM-dd following is the code:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(todaysDate);
Format formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm");
Date date;
try {
date = (Date)((DateFormat) formatter).parse("2011-04-13 05:00");
formatter = new SimpleDateFormat("yyyy-MM-dd");
String s = formatter.format(date);
System.out.println(s);
} catch (ParseException e) {
e.printStackTrace();
}
Use SimpleDateFormat
String myDateString = "2009-04-22 15:51";
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(outFormat.format(inFormat.parse(myDateString)));
If you're getting a date in the format "YYYY-mm-dd hh:mm" and you want it as "YYYY-mm-dd" I suggest you just use inputDate.substring(0, 10).
In either way, beware of potential Y10k bugs :)
Following sample formate date as yyyy-MM-dd in Java
Format formatter = new SimpleDateFormat("yyyy-MM-dd");
Calendar now = Calendar.getInstance();
System.out.println("Now: "+formatter.format(now.getTime()) );
Yes, SimpleDateFormat is what you are looking for
http://dlc.sun.com.edgesuite.net/jdk/jdk-api-localizations/jdk-api-zh-cn/builds/latest/html/en/api/java/text/SimpleDateFormat.html
SimpleDateFormat is what you're looking for.
Try this:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(todaysDate);
Use this code:
Date date=new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(date);
System.out.println("formatted time==>" + formattedDate);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Other answers such as the one by user2663609 are correct.
As an alternative, the third-part open-source replacement for the java.util.Date/Calendar classes, Joda-Time, includes a built-in format for your needs.
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;
String stringIn = "2011-04-07";
// Returns a formatter for a full date as four digit year, two digit month of year, and two digit day of month (yyyy-MM-dd).
DateTimeFormatter formatter = ISODateTimeFormat.date().withZone( DateTimeZone.forID( "Europe/London" ) ).withLocale( Locale.UK );
DateTime dateTime = formatter.parseDateTime( stringIn ).withTimeAtStartOfDay();
String stringOut = formatter.print( dateTime );
Dump to console…
System.out.println( "dateTime: " + dateTime.toString() );
System.out.println( "stringOut: " + stringOut );
When run…
dateTime: 2011-04-07T00:00:00.000+01:00
stringOut: 2011-04-07
This question has so many good answers !! , here comes another one more generic solution
public static String getDateInFormate(String oldFormate , String newFormate , String dateToParse){
//old "yyyy-MM-dd hh:mm"
//new yyyy-MM-dd
//dateTopars 2011-04-13 05:00
String formatedDate="";
Format formatter = new SimpleDateFormat();
Date date;
try {
date = (Date)((DateFormat) formatter).parse(dateToParse);
formatter = new SimpleDateFormat(newFormate);
formatedDate = formatter.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return formatedDate;
}
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String strDate = entry_date;
System.out.println("strDate*************"+strDate);
Date date = null;
try {
date = sdf.parse(strDate);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date yesterday =subtractDay( date);
String requiredDate = df.format(yesterday);
System.out.println("110 days before*******************"+requiredDate);
public static Date subtractDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, -110);`enter code here`
return cal.getTime();
}
java.time
I recommend that you use java.time, the modern Java date and time API, for your date and time work.
For parsing input define a formatter:
private static final DateTimeFormatter FORMATTER
= DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm", Locale.ROOT);
Parse:
String input = "2019-01-21 23:45";
LocalDateTime dateTime = LocalDateTime.parse(input, FORMATTER);
System.out.println(dateTime);
Output so far:
2019-01-21T23:45
Format output:
String output = dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(output);
2019-01-21
Tutorial link
Trail: Date Time (The Java™ Tutorials) explaining how to use java.time.