I have been developing application with GWT on Java. I used calendar tool for my project on this site. When i select any date, give this format (23-Aug-13) to me.
I use this code:
final DateField txtBirthofDay = new DateField("Birth of Day", "dob", 190);
txtBirthofDay.setValue(new Date());
I want to insert this date to database. So, I have to convert this format (2013-08-23). I converted lots of date format each other before but I didn't convert string type (Aug).
Reference these formats Java Date Format Docs:
try this:
SimpleDateFormat dt = new SimpleDateFormat("dd-MMM-yyyy");
Date date = dt.parse("23-Aug-2013");;
// *** same for the format String below
SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(dt1.format(date));
SimpleDateFormat formats your Date to the given pattern
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(new Date()));
Try this,
DateFormat df=new SimpleDateFormat("dd-MMM-yy");
Date date=df.parse("23-Aug-13");
System.out.println(df.format(date));
df=new SimpleDateFormat("yyyy-MM-dd");
String requiredFormatedDate=df.format(date);
System.out.println(requiredFormatedDate);// 2013-08-23
Related
I'd like to calculate the time difference between two dates, but I'm unable to parse a string representation of a date that looks like 2015-01-13 15:59:10, for example, through a formatter SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Only string dates in the form, String dateStart = "01/14/2012 09:29:58";, for example work.
How should I go about parsing 2015-01-13 15:59:10, for example through SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");?
Your format needs to match the literal you're trying to parse:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
instead of
format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Your format must match your input string. The correct format is:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
See the documentation for more details and an explanation.
to parse 2015-01-13 15:59:10 you should change the date format to below:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
I have a string containing date with timezone like
2014-01-24 09.05.14 -06:00.
I need to convert this into date format as YYYYMMDD.
What is the best way in Java to convert the above string into date.
Do it like this (requires JDK 7+):
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss X");
DateFormat outputFormat = new SimpleDateFormat("yyyyMMdd");
String dateString = "2014-01-24 09.05.14 -06:00";
Date date = inputFormat.parse(dateString);
System.out.println(outputFormat.format(date));
One way of doing it is to convert the String into a Date and then use a DateFormatter (e.g. the SimpleDateFormat) to formate that date.
Take a look at this Tutorial for further information.
I've received this start_time from my facebook query:
2013-05-30T19:30:00+0300
how can i parse it to Date in Android ?
I've tried this:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss' '");
Use yyyy-MM-dd'T'HH:mm:ssZ format as below...
String dateString = "2013-05-30T19:30:00+0300";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date date = dateFormat.parse(dateString);
dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
String formatedDate = dateFormat.format(date);
Log.d("Date", formatedDate);
Output:
03-10 18:29:47.074: D/Date(27257): 2013-05-30 10:30
Try out this link:
https://stackoverflow.com/a/13607418/3110609
You need to use SimpleDateFormat but need to manage correctly
I am trying to in insert date into sqlite using:
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String data = format.format(new Date(19900101));
System.err.println("Time is.."+format.format(19900101));
But i am getting 19700101 in log. i am getting same result even if i change the dates as 20000101 or any thing.
Any one suggest me why it happens?
the code is wrong
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Date data = format.parse("19900101");
System.out.println("Time is.."+data );
when u use new Date(123) it expects some time in milliseconds i think
Change your code as for formatting current string to date :
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Date convertedDate = (Date) format.parse("19900101");
SimpleDateFormat formats = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String fmconvertedDate =formats.format(convertedDate);
System.err.println("Time is.."+fmconvertedDate);
and output is :
Time is..1990-01-01 00:00:00
I am working on Java. I have a date for example: 20120328.
Year is 2012, month is 03 & day is 28.
I want to convert it into yyyy-MM-dd format.
I have tried it but it is not working.
How to do it?
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date date = new Date();
String ourformat = formatter.format(date.getTime());
If you want other than "MM/dd/yyyy" then just change the format in SimpleDateFormat
Find some examples here.
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
Date date = (Date)formatter.parse("01/29/02");
Take a look here. You will need to use the SimpleDateFormat class.
First you create the format using substring like
String myDate = "20120328";
String myConvertedDate = myDate.substring(0,4) + "-" + myDate.substring(5,6) + "-" + myDate.substring(7);
This produces the date as 2012-03-28
Then use simple date format to convert that into date if required.
You can use this one for Date formatting
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date date = new Date();
String ourformat = formatter.format(date.getTime());
And this one for getting TimeStamp
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Timestamp timestamp =timestamp=new Timestamp(System.currentTimeMillis());
String ourformat = formatter.format(timestamp);