Let us suppose we have a date show as.
2015-08-03 12:00:00
How would I convert that to a day's name like Tuesday ? I don't want things like 03 Tue etc. Just the full days name. I looked around but I am bit confused on that.
First, parse that date into a java.util.Date object.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date yourDate = formatter.parse("2015-08-03 12:00:00");
Then, populate a Calendar with this date:
Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
Now you have your day of week dayOfWeek (1 being SUNDAY, for example).
SimpleDateFormat simpleDateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = simpleDateformat.parse("2015-08-03 12:00:00");
simpleDateformat = new SimpleDateFormat("EEEE"); // the day of the week spelled out completely
System.out.println(simpleDateformat.format(now));
This is the solution I came up with:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");
Date weekDay = null;
try {
weekDay = formatter.parse("2015-08-03 12:00:00");
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat outFormat = new SimpleDateFormat("EEEE");
String day = outFormat.format(weekDay);
Related
String startDateStr = "2017-02-03"
DateFormat formatter = new SimpleDateFormat("yyyy-MM-DD",Locale.US);
Date date = (Date)formatter.parse(startDateStr);
2017-02-03 date is parsed to Tue Jan 03 00:00:00 GMT+05:45 2017
Did I
miss something?
Update
I needed a string to be converted to a date object
while maintaining the same format.
The reason for this is I want to make use of public boolean after(Date when) method
This will work ^_^
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat outputFormat = new SimpleDateFormat("dd MMM yyyy");
String startDateStr ="2017-02-03";
Date date = null;
try {
date = inputFormat.parse(startDateStr);
String startDateStrNewFormat = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
Little explanation of your output :
D is Day in year (1-365)
d is day in month (1-31)
Check the document
Use SimpleDateFormat type for fomatter. You are creating DateFormat object but using SimpleDateFormat.
String startDateStr = "2017-02-03"
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
Date date = (Date)formatter.parse(startDateStr);
Yes you missed something. You used DD instead of dd in your yyyy-MM-DD format string. Here is how you do it:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(new Date());
How to get the date from the date format
dd/MM/yyyy
Example:
04/05/2015
I only need the date as 04.
Here is code snippet:
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH,1);
System.out.println(dateFormat.format(date));
How to solve this issue?
Use cal.get(Calendar.DAY_OF_MONTH).
You need to change pattern which shows to you desired data format:
DateFormat dateFormat = new SimpleDateFormat("dd");
instead of
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
UPDATE:
Java 1.8 has updated data and time API.
Here is snippet of code which I checked:
LocalDate lastAprilDay = LocalDate.of(2014, Month.APRIL, 30);
System.out.println("last april day: " + lastAprilDay);
LocalDate firstMay = lastAprilDay.plusDays(1);
System.out.println("should be first may day: " + firstMay);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd");
String formatDate = formatter.format(firstMay);
System.out.println("formatted date: " + formatDate);
Output:
last april day: 2014-04-30
should be first may day: 2014-05-01
formatted date: 01
For more info see Java documentations to this classes:
LocalDate
DateTimeFormatter
Simply change DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); to DateFormat dateFormat = new SimpleDateFormat("dd");
or use cal.get(Calendar.DAY_OF_MONTH);
For getting Current date in mm/dd/yyyy format I am using the below code
SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
Date date = new Date();
String date3= sdf.format(date);
date = sdf.parse(date3);
but everytime I print the date ,it gives me wrong and random output
output
Currentd Date:: 49/22/2013
output
Currentd Date:: 07/22/2013
Kindly suggest as what I should use to get current date.
The Java Version I am using is 1.4
Change "mm/dd/yyyy" into "MM/dd/yyyy". m(lowercase) is use for minutes not for month. For month you should use M(uppercase)
You might want to use MM instead of mm in the format pattern which will give you month instead of minutes.
Use MM/dd/yyyy
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
MM - Month
mm - Minute
m = Minute
M = Month
Thus you have to use "MM/dd/yyyy"
Try
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
System.out.println(sdf.format(date));
Try
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime())); //2014/08/06 16:00:22OR
Calendar cal = Calendar.getInstance();
System.out.println(cal.getTime()); //2014/08/06 16:00:22
I am trying to conver the string to a date
String date = "12/31/2012";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
try {
Date parse = sdf.parse(date);
System.out.println(parse);
} catch (ParseException ex) {
ex.printStackTrace();
}
But it appears to me that it is having an Exception. of ParseExpception. why is that? I want to generate a date for 12/31/2012
Date String - 12/31/2012
It matches with - MM/dd/yyyy
d - Day of the month
M - Month in year
y - Year
...
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
format for month is MM not mm and also your should should delimit with - and not / as your formatter delimiter is / and your format should match your date atring.
date 12-31-2012
fmt MM-dd-yyyy
String date = "12-31-2012";
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
If You could use Joda time
String dateStr = "11/15/2013";
Date date = DateTimeFormat.forPattern("dd/MM/yyyy").parseDateTime(dateStr).toDate();
How to store date and time in different variable from this type of date in java
Aug 29 2011 2:24PM
i want store date = 8/29/2011 and time = 2:24PM
hows it possible?
Try This
String inputDateInString = "Aug 29 2011 2:24PM";
DateFormat formatter = new SimpleDateFormat("MMM dd yyyy h:mmaa");
try {
Date dateObject = formatter.parse(inputDateInString);
String date = new SimpleDateFormat("dd/MM/yyyy").format(dateObject);
String time = new SimpleDateFormat("h:mmaa").format(dateObject);
} catch (ParseException e) {
e.printStackTrace();
}
Here is an example using the SimpleDateFormat class
Date today = new Date();
DateFormat format = new SimpleDateFormat("MM/dd/yyyy");
DateFormat year = new SimpleDateFormat("yyyy");
DateFormat month = new SimpleDateFormat("MM");
DateFormat day = new SimpleDateFormat("dd");
System.out.println("today is: " + format.format(today));
System.out.println("The year is: " + year.format(today));
System.out.println("The month is: " + month.format(today));
System.out.println("The day is: " + day.format(today));
Actually, your question is not very clear, but I assume you want to get day, month, etc fields from the date in String. So use SimpleDateFormat to achieve this.
String date="Aug 29 2011 2:24PM";
DateFormat format = new SimpleDateFormat("MMM dd yyyy HH:mm");
Date dt= format.parse(date);
Calendar calendar=Calendar.getInstance();
calendar.setDate(dt);
int d= calendar.get(Calendar.DAY_OF_MONTH);
If you want to get hold of the Year, Month, Week, Day etc, the Calendar class would be what you are looking for.
Calendar c = Calendar.getInstance();
System.out.println("Year: " + c.get(Calendar.YEAR));
System.out.println("Month: " + c.get(Calendar.MONTH));
....