How do I format JDateChooser into dd-mm-yyyy? - java

I am trying to output the current date format into:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");, but it is outputting like this:
Fri Dec 02 14:03:59 AEST 2016
Here is my code:
JDateChooser datePurchased = new JDateChooser();
DateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
Date newDate = new Date();
datePurchased.setDate(newDate);
I am now printing the result like this:
System.out.println(newDate.toString());
But this does not print out what I want, as per above.
My goal output is: 02/12/2016, how do I go about doing this, I've tried looking around but I cannot find the likes to solve my problem.
Thank you in advance.

The first part is simple enough, a Date is a representation in epoch time and doesn't have a modifiable format. Instead, you format it when you want to display it (or otherwise obtain a String representation). Additionally, you need M for months (m is minutes) and if you want / use that instead of -. For example,
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date newDate = new Date();
System.out.println(dateFormat.format(newDate));

You are printing the date but no using the formatter, you need to do:
String pattern = "dd-MM-yyyy";
DateFormat formatter = new SimpleDateFormat(pattern);
System.out.println(formatter.format(newDate));
edit:
if your goal output is: 02/12/2016
then in the pattern in the format incorrect, you will need to use slash and not hyphens
use instead dd/mm/yyyy

Use this simple code:
bill_date.setDateFormatString("yyyy-MM-dd");
In here bill_date is instance of the JDateChooser.

Related

Date value converted from Calendar outputs different format than String [duplicate]

I have the following scenario :
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(dateFormat.parse("31/05/2011"));
gives an output
Tue May 31 00:00:00 SGT 2011
but I want the output to be
31/05/2011
I need to use parse here because the dates need to be sorted as Dates and not as String.
Any ideas ??
How about:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(dateFormat.format(dateFormat.parse("31/05/2011")));
> 31/05/2011
You need to go through SimpleDateFormat.format in order to format the date as a string.
Here's an example that goes from String -> Date -> String.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = dateFormat.parse("31/05/2011");
System.out.println(dateFormat.format(date)); // prints 31/05/2011
// ^^^^^^
Use the SimpleDateFormat.format
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
String sDate= sdf.format(date);
You can use simple date format in Java using the code below
SimpleDateFormat simpledatafo = new SimpleDateFormat("dd/MM/yyyy");
Date newDate = new Date();
String expectedDate= simpledatafo.format(newDate);
It makes no sense, but:
System.out.println(dateFormat.format(dateFormat.parse("31/05/2011")))
SimpleDateFormat.parse() = // parse Date from String
SimpleDateFormat.format() = // format Date into String
If you want to simply output a date, just use the following:
System.out.printf("Date: %1$te/%1$tm/%1$tY at %1$tH:%1$tM:%1$tS%n", new Date());
As seen here. Or if you want to get the value into a String (for SQL building, for example) you can use:
String formattedDate = String.format("%1$te/%1$tm/%1$tY", new Date());
You can also customize your output by following the Java API on Date/Time conversions.
java.time
Here’s the modern answer.
DateTimeFormatter sourceFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu");
DateTimeFormatter displayFormatter = DateTimeFormatter
.ofLocalizedDate(FormatStyle.SHORT)
.withLocale(Locale.forLanguageTag("zh-SG"));
String dateString = "31/05/2011";
LocalDate date = LocalDate.parse(dateString, sourceFormatter);
System.out.println(date.format(displayFormatter));
Output from this snippet is:
31/05/11
See if you can live with the 2-digit year. Or use FormatStyle.MEDIUM to obtain 2011年5月31日. I recommend you use Java’s built-in date and time formats when you can. It’s easier and lends itself very well to internationalization.
If you need the exact format you gave, just use the source formatter as display formatter too:
System.out.println(date.format(sourceFormatter));
31/05/2011
I recommend you don’t use SimpleDateFormat. It’s notoriously troublesome and long outdated. Instead I use java.time, the modern Java date and time API.
To obtain a specific format you need to format the parsed date back into a string. Netiher an old-fashioned Date nor a modern LocalDatecan have a format in it.
Link: Oracle tutorial: Date Time explaining how to use java.time.
You already has this (that's what you entered) parse will parse a date into a giving format and print the full date object (toString).
This will help you.
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
print (df.format(new Date());
I had something like this, my suggestion would be to use java for things like this, don't put in boilerplate code
This looks more compact. Finishes in a single line.
import org.apache.commons.lang3.time.DateFormatUtils;
System.out.println(DateFormatUtils.format(newDate, "yyyy-MM-dd HH:mm:ss"));

SimpleDateFormat parsing date string incorrectly

I'm trying to parse a String into a Date and then format that Date into a different String format for outputting.
My date formatting code is as follows:
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat dateParser = new SimpleDateFormat("d/M/yyyy h:m:s a");
String formattedDocumentDate = dateFormatter.format(dateParser.parse(sysObj.getString("document_date")));
The result of sysObj.getString("document_date") is 1/31/2013 12:00:01 AM. And when I check the value of formattedDocumentDate I get 01/07/2015.
Any help is much appreciated.
You are parsing days 31 as months. SimpleDateFormat tries to give you a valid date. Therefore It adds to 1/0/2013 31 months. This is 2 years and 7 month. So you get your result 01/07/2015. So SimpleDateFormat works correct.
One solution for you is to change your date pattern to M/d/yyyy h:m:s a or your input data.
To avoid these tries you have to switch off SimpleDateFormat lenient mode. Then you will get an exception if the format does not fit.
It looks like your input format is actually months first, then days. So should be "MM/dd/yyyy".
So:
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/M/yyyy");
SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String formattedDocumentDate = dateFormatter.format(dateParser.parse(sysObj.getString("document_date")));
Another example like this
SimpleDateFormat sdfInput = new SimpleDateFormat("yyyyMMdd");
System.out.println("date is:"+new java.sql.Date( sdfInput.parse("20164129").getTime() ));
Output is: 2019-05-29
I expect to throw parse exception but not (41)is not a valid month value.
on the other hand if I gave 20170229, system can recognize the February of 2017 doesn't have a lap year and return 2017-03-01 interesting.

Converting string into date type

I have used the Calendar class to get the current Date. Now I want to convert that date to Date format so that it can be stored in database with format "yyyy.mm.dd". I tried to convert this using SimpleDateFormat class
String dateString = dateText.getText();
SimpleDateFormat dateFormat = new SimpleDateFormat(" yyyy.mm.dd ");
Date convertedDate = dateFormat.parse(dateString);
but I couldn't convert into Date type.
Try to remove spaces from the format string
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.mm.dd");
Also if your input date has invalid format you might get a parse exception. Better if you put it into try/catch block.
Notice, that m stands for minute in hour but M for month of year. Make sure you put a valid format pattern.
You havent stated what the error is but its unlikely that you want to use a minute field to parse the month. Use uppercase M:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
get rid of the whitespaces in your pattern

Can not convert a string to proper date format

In my HTML part I have text box where the date is entered as: 10.09.2013 (dd.mm.yyyy)
What I'm doing in my servlet is:
SimpleDateFormat formatter = new SimpleDateFormat("dd.mm.yyyy");
String dateInString = invoiceData.getString("date");
Date date = formatter.parse(dateInString);
System.out.println(date);
The system prints: Thu Jan 10 00:09:00 EET 2013 and inside of the oracle database it's inserted as 10-SEP-2013
Please help me! I'm dealing with it for about 2 hours and I really can not find a way to convert it and insert it correctly in my database. I know that I'm missing something super small, but I'm unable to spot it.
I'm using Jboss 7.1 server.
new SimpleDateFormat("dd.MM.yyyy");
instead of
new SimpleDateFormat("dd.mm.yyyy");
mm stands for Minutes. MM stands for Month
Take a look date format
mm is used for minutes and MM is used for Month
SO you need to change:
new SimpleDateFormat("dd.MM.yyyy");
mm stands for minutes.
You need to use MM in your pattern to get the month.
mm for Minutes and MM for Month. Change the format to dd.MM.yyyy get the correct date.
Change your code like below:
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
String dateInString = invoiceData.getString("date");
Date date = formatter.parse(dateInString);
System.out.println(date);
use following format
new SimpleDateFormat("dd.MM.yyyy");

java Date and time format

how to format "2011-10-25T13:00:00Z" string into date and time
i used simple date format class
SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
but it only giving the date value. not time values
please help me to solve this problem
Use the format "yyyy-MM-dd'T'HH:mm:ss'Z'" for parsing this date format. See the documentation of SimpleDateFormat for more info. Code will look like this
String dateStr = "2011-09-19T15:57:11Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
Date date = new SimpleDateFormat(pattern).parse(dateStr);
This is because "yyyy-MM-dd" only mentions year (yyyy), month (MM) and date (dd). Try adding hh:mm if you want hours and minutes.
Example:
SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd hh:mm");
System.out.println(sim.format(new Date())); // prints "2011-10-27 01:56"
The full documentation of the format-string and its parts is found here. The documentation includes this example:
"yyyy-MM-dd'T'HH:mm:ss.SSSZ" - 2001-07-04T12:08:56.235-0700
Perhaps it's something like that you're looking for.

Categories

Resources