How to format this string do a proper date format? - java

I'm very new to Java programming and I have string like this:
2013-03-15T07:23:13Z
I wish I could convert this into date format like:
15-03-2013
is that possible?
Thanks in advance.

Take the reference to this link
How can I change the date format in Java?
See the answer given by Mr. Christopher Parker
It has explained all your needs and it will provide you the easiest solution which is logically correct

Try this :
try {
DateFormat sourceDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
DateFormat targetFormat = new SimpleDateFormat("dd-MM-yyyy");
String strSourceDate = "2013-03-15T07:23:13Z";
Date targetDate = (Date) sourceDateFormat.parseObject(strSourceDate);
String strTargetDate = targetFormat.format(targetDate);
System.out.println(strTargetDate);
} catch (ParseException e) {
e.printStackTrace();
}

If the format of the input string is fixed, the simplest and the most expedient way of doing this would be with string manipulation:
String s = "2013-03-15T07:23:13Z";
String res = s.substring(8, 10)+"-"+s.substring(5, 7)+"-"+s.substring(0, 4);
It would spare you dealing with dates and calendars. Here is a demo on ideone.

java.text.SimpleDateFormat is what you need: SimpleDateFormat JavaDoc
You'll need one format to convert your input String into a Date using the parse() method, and then another to convert that Date into a String in your desired format using format().
If your application could be used internationally, don't forget to think about correctly localizing the output of the second function though. 03-11-2013 is March 11th in some countries, November 3rd in others.

Try this:
Date dNow = new Date( );
SimpleDateFormat ft =
new SimpleDateFormat ("dd.MM.yyyy");
System.out.println("Current Date: " + ft.format(dNow));
It's output
Current Date: 15.03.2013

Using SimpleDateFormat
String strDate = "2013-03-15T07:23:13Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String date = dateFormat.format(strDate);
System.out.println("Today in dd-MM-yyyy format : " + date);
Hope it help you...

Related

How to format a jQuery date using SimpleDateFormat

I am using jQuery Datepicker that is giving the date like 07/05/2015 this format.I am using simpledateformat to format this date.But always the SDF is converting it to the todays date.How to solve this ??
System.out.println("Activity IS : IS With Date");
SimpleDateFormat sdfOverTimeWithDate = new SimpleDateFormat("yyyy-MM-dd ");
Date startDate = ParamUtil.getDate(resourceRequest, "startDate", sdfOverTimeWithDate);
Date endDate = ParamUtil.getDate(resourceRequest, "endDate", sdfOverTimeWithDate);
int jobId= ParamUtil.getInteger(resourceRequest, "jobId");
System.out.println("jobId :"+jobId);
System.out.println("startDate :"+startDate);
System.out.println("endDate :"+endDate);
This statDate and endDate is giving todays's date only ,while the date i am pssing is in the format 07/05/2015.How to solve this ??somebody plaese help
You are passing wrong date format to SimpleDateFormat constructor. Try "dd/MM/yyyy" instead of "yyyy-MM-dd "
Is "07/05/2015" in July (US-Format) or May? Please clarify. If it is US-format (date expression starting with month number) then your solution is to use the pattern "MM/dd/yyyy" otherwise you should use the pattern "dd/MM/yyyy".
The pattern "yyyy-MM-dd " cannot be right due to two reasons:
a) It starts with a four-digit-year but your input begins with a two-digit-number.
b) It contains a trailing space.
Try to do it.
SimpleDateFormat sdfOverTimeWithDate = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d = sdfOverTimeWithDate.parse("07/05/2015");
} catch (ParseException e) {
e.printStackTrace();
}

Setting date into "dd"-"mm"-"yy" format from "dd"-"mm"-"yyyy" in java

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

Parse A Java Date

How can I parse the following date in Java from its string representation to a java.util.Date?
2011-07-12T16:45:56
I tried the following:
private Date getDateTime(String aDateString) {
Date result = new java.util.Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
try
{
result = sf.parse(aDateString);
}
catch (Exception ex)
{
Log.e(this.getClass().getName(), "Unable to parse date: " + aDateString);
}
return result;
}
You are not using the right date format pattern. The year/month/day separators are clearly wrong, and you need a literal 'T'. Try this:
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
For the record, this is an ISO 8601 combined date and time format.
Your date format pattern is incorrect. It should be yyyy-MM-dd'T'HH:mm:ss .
To parse a date like "2011-07-12T16:45:56" you need to use:-
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Check the pattern you are feeding your SimpleDateFormat against the string you are feeding in.
See any potential discrepancies? I see 3.

How to format a date String into desirable Date format

I was trying to format a string into date.
For this I have written a code:-
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(sdf.format( cal.getTime() ));
This is fine..
But now I want to convert a string into a date formatted like above..
For example
String dt="2010-10-22";
And the output should be like this:-
2010-10-22T00:00:00
How do I do this?
String dt = "2010-10-22";
SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition ps = new ParsePosition(0)
Date date = sdfIn.parse(dt, pos)
SimpleDateFormat sdfOut = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(sdfOut.format( date ));
This should do it for you, remember to wrap it in a try-catch block just in case.
DateFormat dt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
try
{
Date today = dt.parse("2010-10-22T00:00:00");
System.out.println("Your Date = " + dt.format(today));
} catch (ParseException e)
{
//This parse operation may not be successful, in which case you should handle the ParseException that gets thrown.
//Black Magic Goes Here
}
If your input is going to be ISO, you could also look at using the Joda Time API, like so:
LocalDateTime localDateTime = new LocalDateTime("2010-10-22");
System.out.println("Formatted time: " + localDateTime.toString());
The same class you use for output formatting of dates can also be used to parse dates on input.
SimpleDateFormat reference
To use your example, to parse the sample date:
String dt = "2010-10-22";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(dateFormatter.parse(dt));
The fields that are not specified (ie. hour, minutes, etc) will be 0. So your same code can be used to format the date on output.
Date Format Example
Containing the Conversion of String Date object from one format to another

Java date format real simple

how would you write te date if i have a date and all i want is the month and the day like this (mm/dd) and then turn the month like this July, 08
Let me see if I understood well.
You have a date like "07/08" and you want "July, 08"?
You could try SimpleDateFormat
import java.text.SimpleDateFormat;
import java.text.ParseException;
class Test {
public static void main( String [] args ) throws ParseException {
SimpleDateFormat in = new SimpleDateFormat("MM/dd");
SimpleDateFormat out = new SimpleDateFormat("MMMM, dd");
System.out.println( out.format( in.parse("07/08") ) );
// Verbose
//String input = "07/09";
//Date date = in.parse( input );
//String output = out.format( date );
//System.out.println( output );
}
}
Use:
Format formatter = new SimpleDateFormat("MMMM, dd");
String s = formatter.format(date);
Formatting a Date Using a Custom Format
The SimpleDateFormat is your friend here. If you already have a java.util.Date object, just format it using the desired pattern (refer to the javadoc for details on date and time patterns):
SimpleDateFormat out = new SimpleDateFormat("MMMM, dd");
String s = out.format(date); // date is your existing Date object here
(EDIT: I'm adding some details as the original question is unclear and I may have missed the real goal.
If you have a String representation of a date in a given format (e.g. MM/dd) and want to transform the representation, you'll need 2 SimpleDateFormat as pointed out by others: one to parse the String into a Date and another one to format the Date.
SimpleDateFormat in = new SimpleDateFormat("MM/dd");
Date date = in.parse(dateAsString); // dateAsString is your String representation here
Then use the code snippet seen above to format it.)
the month and the day like this (mm/dd) and then turn the month like this July, 08
So you want to convert MM/dd to MMMM, dd? So you start with a String and you end up with a String? Then you need another SimpleDateFormat instance with the first pattern.
String dateString1 = "07/08";
Date date = new SimpleDateFormat("MM/dd").parse(dateString1);
String dateString2 = new SimpleDateFormat("MMMM, dd").format(date);
System.out.println(dateString2); // July, 08 (monthname depends on locale!).

Categories

Resources