Help needed in formatting date in Java - java

Hi I have the following date as String format.
Input
2010-04-20 05:34:58.0
Output I want the string like
20, Apr 2010
Can someone tell me how to do it ?

Try this:
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
DateFormat outputFormat = new SimpleDateFormat("dd, MMM yyyy");
Date yourDate = inputFormat.parse("2010-04-20 05:34:58.0");
String formattedDate = outputFormat.format(yourDate);

You might want to try this:
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date date = inFormat.parse( "2010-04-20 05:34:58.0");
SimpleDateFormat outFormat = new SimpleDateFormat("dd, MMM yyyy");
System.out.println(outFormat.format( date));

Related

How to get proper written date using SimpleDateFormat in Java

I have this:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date result = dateFormat.parse(this.getCreatedTime());
Basically I want to convert a string like "2016-09-27T09:19:57Z" into something like "September 27, 2016 at 9:19 AM".
If I use the code above I end up with a Date object, but all the methods are deprecated. So how do I achieve this?
You can use DateFormat again as #Thomas wrote:
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date inputDate = inputFormat.parse(this.getCreatedTime());
DateFormat outputFormat = new SimpleDateFormat("outputFormat");
String output = outputFormat.format(inputDate);
You should do research before posting any question here.
Use this to get Date from your required pattern
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
Date date = null;
try {
date = format.parse(unformattedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Make new instance of your desired patter.
format = new SimpleDateFormat("MMMM dd, yyyy 'at' HH:mm a", Locale.getDefault());
String formattedDate = format.format(date);

Convert Current Timestamp to DD MMM YYYY format

I am trying to convert current timestamp to DD MMM YYYY format.
but i don't know why it is giving me an error unparsable.
code:
Timestamp ts = new Timestamp(new Date().getTime());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date fechaNueva = format.parse(ts.toString());
System.out.println(format.format(fechaNueva));
working but I want to do
SimpleDateFormat format = new SimpleDateFormat("dd MMM YYY, HH:mm");
gives me error of unparsable.
java.text.ParseException: Unparseable date: "2014-10-07 15:38:29.876"
Following worked for me
Timestamp ts = new Timestamp(new Date().getTime());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date fechaNueva = format.parse(ts.toString());
format = new SimpleDateFormat("dd MMM YYY, HH:mm");
System.out.println(format.format(fechaNueva));
Output:
07 Oct 2014, 15:46
You were trying to parse date in a format you wanted it to be formatted instead of format in which it is already present.
Simply u can do this
Timestamp timestamp = new Timestamp(new Date().getTime());
System.out.println(timestamp);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM YYY, HH:mm");
System.out.println("Formatted "+dateFormat.format(timestamp));
hope this helps.
OutPut:-
Formatted 07 Oct 2014, 15:59
You can't use ts.toString(), you always need to use the same formatter.
Use just:
Timestamp ts = new Timestamp(new Date().getTime());
SimpleDateFormat format = new SimpleDateFormat("dd MMM YYY, HH:mm");
System.out.println(format.format(ts));

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

How to convert 'dd/mm/yyyy hh:mm:ss am/pm' to hh:mm:ss (without am or pm) in java?

I want the convert the string like 1/15/2014 9:57:03 AM or 1/15/2014 5:49:39 PM to 9:57:03 and 17:49:39
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a", Locale.US);
Date date = format.parse("1/15/2014 9:57:03 AM");
format = new SimpleDateFormat("HH:mm:ss");
String dateString = format.format(date);
BTW, use Google next time
You should see this in the doc
doc SimpleDateFormat

transforming one date string into another java

I looked around here and could not find anything that matches what I need.
I get the following String:
"March 13, 2013"
and need to transform it into
"2013-03-13"
I tried using this code:
SimpleDateFormat inputFormat = new SimpleDateFormat("MMMMMMMMMM dd, YYYY");
SimpleDateFormat outputFormat = new SimpleDateFormat("YYYY-MM-dd");
Date d = inputFormat.parse(startDate);
System.out.println(outputFormat.format(d));
but i get "203-12-30".. Why is that?
Y represents week year. Try using y
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
I just copied and pasted your code made a few small changes and got the result you were looking for. The main change I made was changing the upper case "Y" in inputFormat and outputFormat to lower case "y". Anyway here you are:
String startDate = "March 13, 2013";
SimpleDateFormat inputFormat = new SimpleDateFormat("MMMMMMMMMM dd, yyyy");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d = inputFormat.parse(startDate);
System.out.println(outputFormat.format(d));
} catch (ParseException e) {
e.printStackTrace();
}
Have a try with the following code:
String startDate = "March 13, 2013";
SimpleDateFormat inputFormat = new SimpleDateFormat("MMMM dd, yyyy",Locale.ENGLISH);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
Date d = inputFormat.parse(startDate);
System.out.println(outputFormat.format(d));

Categories