Represent MM-DD-YYYY as Day, Month Date [duplicate] - java

This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 7 years ago.
I want to represent date in the format MM-DD-YYYY as Day, Month Date
For example - represent 5-12-2015 as Tuesday, May 12. How can I do this?

Though I am giving you the code to do this, you should first show us your efforts.
String date = "5-12-2015";
DateFormat format = new SimpleDateFormat("MM-dd-yyyy");
Date d = format.parse(date);
DateFormat format1 = new SimpleDateFormat("EEEE, MMM dd");
String finalDateString = format1.format(d);
System.out.println(finalDateString);

Related

Issue with converting String to Date [duplicate]

This question already has answers here:
Comparing time is incorrect when picking 12:00
(3 answers)
Closed 4 years ago.
I tried to convert a String to Date by the following way in Java:
public static Date dateConv1(String s){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");
String dateInString = s;
Date date = new Date();
It converts the noon 12:00-12:59 as midnight i.e 00:00-00:59. Could soeone let me know how I should solve this issue?
Change your formatter:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
HH - means hours
Reach to: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html and see all the examples you need.
The String is not correctly formated. hh means 1-12 hours. There are 4 way to format hours (hh, HH, kk, KK).
Here you can read about the formatting:
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");
" hh " is for 12 hours format change it to
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

dd/MM/yyyy Date Format to Monthname day,Year Java Android [duplicate]

This question already has answers here:
Parse String to Date with Different Format in Java
(10 answers)
Closed 4 years ago.
First i have a java Date object lets say
Date firstdate; // its initialized and equal to some date dont worry
with using this code i convert it to dd/MM/yyyy format
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
date.setText(sdf.format(firstdate));
But since sdf.format is returning to a string,after formating 1 time i cant format it again to my desired Monthname day,Year format before displaying to user.
i mean if sdf.format(firstdate) returns "01/01/2000" string,i would like to convert this to January 1,2000 and then display it to user.
First get the String and convert to a Date:
String s = date.getText(); // e.g. "01/01/2000"
Date d = sdf.parse(s); // parse the String to get a Date object
Then you need a second SimpleDateFormat to convert the Date to the desired String:
SimpleDateFormat sdf2 = new SimpleDateFormat("MMMMM dd, yyyy");
s = sdf2.format(d); // "January 1, 2000"

Converting Date to String in SimpleDateFormat [duplicate]

This question already has answers here:
How to convert current date into string in java?
(9 answers)
Closed 6 years ago.
I want to convert the date format to string, but the string output is not coming
DateFormat currentDate = new SimpleDateFormat("dd MM yyyy ");
String currDateString = String.valueOf(currentDate);
System.out.println(currDateString);
To format current date you need to create a instance of Date which represents current date and then format it using SimpleDateFormat to string.
DateFormat dateFormat = new SimpleDateFormat("dd MM yyyy");
String currDateString = dateFormat.format(new Date());
System.out.println(currDateString);

SimpleDateFormat and Date object [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Month name as a string
(12 answers)
Closed 6 years ago.
In my android app I retrieve from an api the current day as.
long dateTime = innerJSON.getLong("dt");
Date weekDay = new Date(dateTime * 1000L);
SimpleDateFormat outFormat = new
SimpleDateFormat("EEEE");
String day = outFormat.format(weekDay);
And I get the current day ie Monday.
For the date I use the same Date object but with a different SimpleDateFormat object.
SimpleDateFormat outFormat1 = new SimpleDateFormat("dd-M-yyyy");
String date = outFormat1.format(weekDay);
And that gives me,
28-5-2016
which is fine. However, I want the date to have a format of
28 May
Any ideas on that? I checked the official orarcle's site,but I can't find a solution.
Thanks,
Theo.
Try this
SimpleDateFormat outFormat1 = new SimpleDateFormat("dd MMM");
you will get the output you required.
You need to use dd MMM format here.
More details provided here in official documentation.
SimpleDateFormat outFormat1 = new SimpleDateFormat("dd MMM");
String date = outFormat1.format(weekDay);
haha. That was easy.
SimpleDateFormat("dd MMM");
It's summer now and sunny,so I can't concetrate:):)

Java how to convert UK into java.util.Date [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 7 years ago.
I'm retrieving a date in a HTML form, and I need to convert the string date into a java.util.Date so that it can be inserted into a database.
I've tried the following, but it always gives me a date in May 2008.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = formatter.parse("27-11-2015");
String string = "2015-11-27";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);
This was user error, my code should have been:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date myDate = formatter.parse("27-11-2015");

Categories