I would like to get the date in numbers in Java.
For example: today is 13th of dec, 2012. I would like to get it in numbers as 13.12.2012
How can I achieve it? Any help would be really appreciated.
P.S I tried something like:
Date d = new Date();
String date = d.toString().substring(0, 10);
But got the output as:Thu Dec 13
DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
String formattedDate = df.format(new Date());
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String res = format(d);
You can use SimpleDateFormat to achieve that
new java.text.SimpleDateFormat("dd.MM.yyyy").format(new Date());
Related
I have a date in the format dd-mm-yyyy format(inside my db).I need to convert it to dd-mm-yy format.I retrieved date from db and stored it inside a string.
String date=doc.getString("date");
for example: 05-19-1990
to
05-19-90.
That 1990 is not fully needed only 90 is needed.Using split() in java i tried some thing but it wont helps me.Can anyone please help.Any help will be highly appreciated.......
Use DateFormat for that issue:
DateFormat df = SimpleDateFormat("dd-MM-yyyy");
DateFormat df1 = SimpleDateFormat("dd-MM-yy");
df1.format(df.parse(date));
Try below code
try {
SimpleDateFormat sdf= new SimpleDateFormat("MM-dd-yy");
Date d = sdf.parse("05-19-1990");
System.out.println(sdf.format(d));
} catch (ParseException ex) {
ex.printStackTrace();
}
Use two SimpleDateFormats, one to parse it, one to format it..
SimpleDateFormat in = new SimpleDateFormat("MM-dd-yyyy");
Date inDate = in.parse(date); // 05-19-1990
SimpleDateFormat out = new SimpleDateFormat("MM-dd-yy");
String newDate = out.format(inDate);
or
SimpleDateFormat out = new SimpleDateFormat("dd-MM-yy");
String newDate = out.format(inDate);
If you really want 19-05-90 as per the title of your question ;)
Check java.text.SimpleDateFormat for more details
Try this..
DateFormat df2 = new SimpleDateFormat("dd-mm-yy");
String formattedDate2 = df2.format(theDate);
Having trouble in the following code. The output is dateStr: 11-Jan-11. Can anyone tell me why the date is modified?
String dateStr="";
String actionCompletionDueDate = "16/11/2011";
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
DateFormat destDf = new SimpleDateFormat("dd-MMM-yy");
if(actionCompletionDueDate != null && !actionCompletionDueDate.equals("")) {
// parse the date string into Date object
System.out.println("actionCompletionDueDate: " + actionCompletionDueDate);
Date actionCompletionDate = srcDf.parse(actionCompletionDueDate);
dateStr = destDf.format(actionCompletionDate);
System.out.println("dateStr: " + dateStr);
}
Change
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
to
DateFormat srcDf = new SimpleDateFormat("dd/MM/yyyy");
OR pass a correct string (which respects your format) to your code
String actionCompletionDueDate = "11/16/2011";
and correct the format to DateFormat srcDf = new SimpleDateFormat("MM/dd/yyyy");
mm is for minutes and MM is for months
String actionCompletionDueDate = "16/11/2011";
Should be
String actionCompletionDueDate = "11/16/2011";
Change
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
to
DateFormat srcDf = new SimpleDateFormat("MM/dd/yyyy");
small mm here corresponds to minute.
But if you print source date using,
System.out.println(actionCompletionDate.toString());
Output is :
Sun Jan 16 00:11:00 IST 2011
See, 11 minute in time.
And change source date too, to 11/16/2011.
I'm trying to set a date format, but when i run this code
String oldstring = "2013-01-1";
System.out.println("oldstring = "+oldstring);
Date date = new SimpleDateFormat("yyyy-mm-dd").parse(oldstring);
System.out.println("datefield = "+date);
i take result:
oldstring = 2013-01-1
datefield = Tue Jan 01 00:01:00 MSK 2013
Why datefield isn't equal 2013-01-1?
At first mm in yyyy-mm-dd mean minute not Month. to set month use MM.
It would be look like this :
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(oldstring);
UPDATE
Try this:
String oldstring = "2013-01-1";
System.out.println("oldstring = "+oldstring);
Date date = new SimpleDateFormat("yyyy-mm-dd").parse(oldstring);
String sdf = new SimpleDateFormat("yyyy-mm-dd").format(date);
System.out.println("datefield = "+sdf);
If you don't use new SimpleDateFormat("yyyy-mm-dd").format(date);
you getting standard date format which include all info. If you want special format you need to use
new SimpleDateFormat("yyyy-mm-dd").format(date);
Also read this article about date formatting
The type of datefield is Date, so the toString method will basically always return the same format, as you are not overriding it.
So what you need to do, is basically:
String oldstring = "2013-01-1";
System.out.println("oldstring = "+oldstring);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(oldstring);
System.out.println("datefield = "+date);
String outDateStr = sdf.format(date);
System.out.println("newstring = "+outDateStr);
Use MM for month. mm is for minutes
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
How would I convert a String date in the form of 24/04/2012 to a date variable in the format of 24-Apr-12, which can later be passed into my Oracle database.
I have tried this but it says the string date is unparsable:
DateFormat format = new SimpleDateFormat("dd-MMM-yy");
Date newDate = (Date) format.parse(date);
I think you are confusing parsing and formatting, try this:
String old_date = "24/04/2012";
DateFormat old_format = new SimpleDateFormat("dd/MM/yyyy");
Date newDate = (Date) old_format.parse(old_date);
DateFormat new_format = new SimpleDateFormat("dd-MMM-yy");
String date = new_format.format(newDate);
System.out.println(date);
You are doing this backwards:
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date newDate = format.parse(date);
DateFormat formatOutput = new SimpleDateFormat("dd-MMM-yyyy");
String output = formatOutput.format(newDate);
But if what you are doing is passing the date to Oracle, you should really use a PreparedStatement
Your date format should be "dd/MM/yyyy"
SimpleDateFormat sdf= new SimpleDateFormat("dd-MM-yy");
DateTime newDate = sdf.format(date);
Try DateFormat format = new SimpleDateFormat("dd/MM/yyyy")
Why don't you try using java.sql.Date.
For example:
Date newDate = new java.sql.Date(date.getTime());
So you can just give an generic sql object for date.
The Date class of Java is quite difficult to use in many cases. I recommend the use of the much better Joda Time classes:
DateTimeFormatter oldFormat = DateTimeFormat.forPattern("dd/mm/yyyy");
DateTimeFormatter newFormat = DateTimeFormat.forPattern("dd-MMM-yy");
DateTime dateTime = oldFormat.parseDateTime("24/04/2012");
String newDate = newFormat.print(dateTime);