Java Formatting Date String - java

I need to convert this 20140815
into 15.08.2014 and into 15.08.2014 00:00:00
How do I do that, what opportunities are there in Java

You could use SimpleDateFormat as follows
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat outputFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
Date date = inputFormat.parse("20140815");
String result = outputFormat.format(date);

Why bother with Dates... just use Strings:
String output = input.replaceAll("(....)(..)(..)", "$3.$2.$1");
or
String output = input.replaceAll("(....)(..)(..)", "$3.$2.$1 00:00:00");

Related

How to format a date

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")

How to parse a Date like this 20-Feb-2006?

i have problem of parsing a String into Date when the month contains 3 letters instead of two
use DateFormat in this way:
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
System.out.println( df.parse("20-Feb-2006"));
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
Date date = df.parse("20-Feb-2006");
USe
String strDate = "20-Feb-2006";
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
Date dateStr = formatter.parse(strDate);

Date convert in Java

I want to compare two dates, for that i convert the string to date format.But during the conversion the date format changed to "02/01/2013" and "03/01/2014".It makes error in my logic.any one please tell me to how to compare two days in my date format.
String fdate="01/02/2012";
String tdate="01/03/2013";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date frmdt=new Date(fdate);
String s1 = sdf.format(frmdt);
Date todt=new Date(tdate);
String s2 = sdf.format(todt);
Date frmdate = sdf.parse(s1);
Date todate = sdf.parse(s2);
if(frmdate.compareTo(todate)<=0){
//process;
}
Try this:
String fs = "01/02/2012";
String ts = "01/03/2013";
DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
sdf.setLenient(false);
Date fdate = sdf.parse(fs);
Date tdate = sdf.parse(ts);
if (fdate.before(tdate) || f.date.equals(tdate)) {
//process;
}
You've got too much going on. It's much simpler.
It seems to me that you should be calling SimpleDateFormat.parse instead:
// Using the US locale will force the use of the Gregorian calendar, and
// avoid any difficulties with different date separator symbols etc.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("UTC")); // Avoid DST complications
sdf.setLenient(false);
Date fromDate = sdf.parse(fromDateText);
Date toDate = sdf.parse(toDateText);
// Alternatively: if (!fromDate.after(toDate))
if (fromDate.compareTo(toDate) <= 0) {
...
}
I'd actually suggest that you use Joda Time if at all possible, where you could use a LocalDate type to more accurately represent your data.
You're doing it wrong :)
String fdate="01/02/2012";
String tdate="01/03/2013";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date frmdate = sdf.parse(fdate);
Date todate = sdf.parse(tdate);
if(frmdate.compareTo(todate)<=0){
//process;
}
You were passing to Date a not parsed date with your format. Also Date(String) is deprecated. DateFormat.parse(String) is the correct one.

Convert date into specific format

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);

How to convert a string to date format in java?

How to convert a String to DateFormat in java?
I am writing a application where I want to convert a string "20050105000200" to "2005-01-05 00:02:00". Is there a direct way to do it in Java? I want both input and output in String. Please let me know if there is a way to do it directly.
Can you give me some simple codes?
Thanks.
You can use SimpleDateFormat to parse the input date, and then again to format the output
SimpleDateFormat inFmt = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat outFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = inFmt.parse("20050105000200");
System.out.println(outFmt.format(d));
You should first parse it to a date like this:
http://www.exampledepot.com/egs/java.text/parsedate.html
and then format it again like this:
http://www.exampledepot.com/egs/java.text/formatdate.html
This example might help you
String str_date="11-June-07";
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MMM-yy");
date = (Date)formatter.parse(str_date);
Use a DateFormat to parse() this String to a Date object. Use a different DateFormat to format() the Date to the String representation you want.
See this
You can use simpledateformat class for doing that
Use SimpleDateFormat. Haven't tried on my IDE, but it goes something like this:
SimpleDateFormat fromUser = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String reformattedStr = myFormat.format(fromUser.parse("20050105000200"));
System.out.println(reformattedStr);

Categories