I need to get the month and day of today's date and offset dates. This is how I do it:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_WEEK, 0);
Date today = calendar.getTime();
System.out.println(today);
Output:
Wed Aug 27 15:07:35 CEST 2014
Two things, I need the month and the day to be numeric, like 8/27. I understand how to do that with today's date like so:
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
String a = String.valueOf(day);
String b = String.valueOf(month);
System.out.println(b +"/" + a);
My issue is that I might need to add an offset to that date, if I want tomorrows date for example. Is there a way to do that because converting Wed Aug 27.... to 8/27 would just be a pain. Thanks
Use simple date format:
Something like:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_WEEK, 1);
Date today = calendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
System.out.println(sdf.format(today));
DateFormat formatter = new SimpleDateFormat("MM/dd");
Calendar cal = Calendar.getInstance();
String calAsString = formatter.format(cal.getTime());
System.out.println(calAsString);
// Now for tomorrow's date:
int offset = 1;
cal.add(Calendar.DATE, offset);
calAsString = formatter.format(cal.getTime());
System.out.println(calAsString);
Use the Calendar to add a value to the day:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH,1)
Related
How can i get String date from calendar?
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MOUNTH, -5); //set now and 5 days to back
I want get String like this(date on interval -5 days to TODAY):
11.03.2015
10.03.2015
.
.
.
07.03.2015
It's possible? How?
you can use for loop and reduce one day from calendar instance and print it
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
for (int index = 1; index <= 5; index++) {
calendar.add(Calendar.DATE, -1);
System.out.println(dateFormat.format(calendar.getTime()));
}
output:
10.03.2015
09.03.2015
08.03.2015
07.03.2015
06.03.2015
You should use the SimpleDateFormat class, as follows.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MOUNTH -5);
SimpleDateFormat myDateFormat = new SimpleDateFormat("MM.dd.yyyy"); //or ("dd.MM.yyyy"), If you want days before months.
String formattedDate = myDateFormat.format(cal.getTime());
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
Long beforeTime = date - (5*24*60*60*1000);
Date beforeDate = new Date(beforeTime);
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
String s = format.format(beforeDate);
s returns the date in your required format.
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String strdate = sdf.format(calendardate.getTime());
I need to get the dates of last two Fridays using today's date.This is the code I'm currently using
public class GetDate {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dt = new Date();
Calendar c = Calendar.getInstance();
c.setTime(dt);
System.out.println("today : " + sdf.format(dt));
while (c.get(Calendar.DAY_OF_WEEK) != 6) {
c.add(Calendar.DATE, -1);
}
Date lastFri=c.getTime();
System.out.println("last fri : "+sdf.format(lastFri));
c.add(Calendar.DATE, -7);
Date prevFri = c.getTime();
System.out.println("previous friday : "+sdf.format(prevFri));
}
}
Is there any way to optimize this code??
With Java 8, and if you don't have to use the Calendar api, you can use a TemporalAdjuster:
LocalDate today = LocalDate.now();
LocalDate prevFriday = today.with(previous(FRIDAY));
LocalDate prevPrevFriday = prevFriday.with(previous(FRIDAY));
note: requires the following static import
import static java.time.temporal.TemporalAdjusters.previous;
import static java.time.DayOfWeek.FRIDAY;
You could get the last Friday this way :
Calendar cal = Calendar.getInstance();
cal.add(Calendar.WEEK_OF_YEAR, -1);
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
This gets the last Friday by subtracting a week and setting the day of week to Friday.
Try something like:
cal.add(Calendar.WEEK_OF_YEAR, -1);
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
Date lastFriday = cal.getTime();
cal.add(Calendar.WEEK_OF_YEAR, -1);
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
Date lastToLastFriday = cal.getTime();
This line should help:
calendar.add(Calendar.DATE, Calendar.FRIDAY - 7 - c.get(Calendar.DAY_OF_WEEK));
So this code is solving your problem:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = GregorianCalendar.getInstance();
c.add(Calendar.DATE, Calendar.FRIDAY - 7 - c.get(Calendar.DAY_OF_WEEK));
System.out.println(sdf.formate(c.getTime()));
c.add(Calendar.DATE, Calendar.FRIDAY - 14 - c.get(Calendar.DAY_OF_WEEK));
System.out.println(sdf.formate(c.getTime()));
I am using the below code
#SuppressWarnings("deprecation")
Date d = new Date (2014,01,9);
System.out.println(d);
DateFormat df = new SimpleDateFormat("yyyyMMdd");
final String text = df.format(d);
System.out.println(text);
I am getting below output.
3914-02-09
39140209
Does any one know why there is 3914?
Thanks,
Mahesh
The javadoc for the constructor you're using java.sql.Date(int,int,int) reads (in part),
year - the year minus 1900; must be 0 to 8099. (Note that 8099 is 9999 minus 1900.)
so you should use (assuming you mean this year)
Date d = new Date (2015-1900,01,9);
From Java Docs,
Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date) or GregorianCalendar(year + 1900, month, date).
Allocates a Date object and initializes it so that it represents midnight, local time, at the beginning of the day specified by the year, month, and date arguments.
Parameters:
year the year minus 1900.
month the month between 0-11.
date the day of the month between 1-31.
Code
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
int year = 2014;
int month = 01;
int day = 9;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DAY_OF_MONTH, day);
java.sql.Date date = new java.sql.Date(cal.getTimeInMillis());
System.out.println(sdf.format(date));
}
output
2014-01-09
I want to compare two date objects like this:
String format = "MM/dd/yyyy hh:mm a";
SimpleDateFormat sdf = new SimpleDateFormat(format);
fakeDate = sdf.parse("15/07/2013 11:00 AM");
fakeDate2 = sdf.parse("15/07/2013 12:00 AM");
int diff = date2.getHours() - date1.getHours();
but then I see getHours is deprecated.
So i have used:
Calendar calendar1 = Calendar.getInstance();
calendar.setTime(new Date("15/7/2013 11:00AM"));
Calendar calendar2 = Calendar.getInstance();
calendar.setTime(new Date("11/7/2013 12:00AM"));
calendar1.get(Calendar.HOUR_OF_DAY) - calendar2.get(Calendar.HOUR_OF_DAY)
but then I see the diff is zero. I guess i change the same calendar instance all the time and compare it to itself. no?
how would you write this?
new Date("15/7/2013 11:00AM")
is not the correct way to construct a Date object. It's deprecated also. Use proper SimpleDateFormat as you are doing well in your first example.
The format MM/dd/yyyy hh:mm a is not correct as per the input date string.
It should be like this.
String format = "dd/MM/yyyy hh:mm a";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date fakeDate = sdf.parse("15/07/2013 11:00 AM");
Date fakeDate2 = sdf.parse("15/07/2013 12:00 AM");
Calendar cal = Calendar.getInstance();
cal.setTime(fakeDate); //set time to first date
int hours1 = cal.get(Calendar.HOUR); // get the hours
cal.setTime(fakeDate2); // set time to second date
int hours2 = cal.get(Calendar.HOUR); // get the hours
System.out.println(hours1 - hours2); // 11 hours
You seemed really confused though it is depreciated this code shall give you a difference of 11 hours which makes sense
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
Date fakeDate = dateFormat.parse("15/07/2013 11:00 AM");
Date fakeDate2 = dateFormat.parse("15/07/2013 12:00 AM");
System.out.println(fakeDate.getHours()-fakeDate2.getHours());
Or if you are so interested to use Calendar do it this way
Calendar calendar = Calendar.getInstance();
calendar.setTime(fakeDate);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(fakeDate2);
System.out.println(calendar.get(Calendar.HOUR_OF_DAY) -
calendar2.get(Calendar.HOUR_OF_DAY));
You will get the same 11 hours difference or simply do it this way
long millisForNewDate = toDate.getTime();
long millisForOldDate = fromDate.getTime();
long difference = millisForNewDate - millisForOldDate;
int hoursDifferenceBetweenDates = (int)(difference/(1000*60*60));
Sample Julian Dates:
2009218
2009225
2009243
How do I convert them into a regular date?
I tried converting them using online converter and I got-
12-13-7359 for 2009225!! Makes no sense!
Use the Joda-Time library and do something like this:
String dateStr = "2009218";
MutableDateTime mdt = new MutableDateTime();
mdt.setYear(Integer.parseInt(dateStr.subString(0,3)));
mdt.setDayOfYear(Integer.parseInt(dateStr.subString(4)));
Date parsedDate = mdt.toDate();
Using the Java API:
String dateStr = "2009218";
Calendar cal = new GregorianCalendar();
cal.set(Calendar.YEAR,Integer.parseInt(dateStr.subString(0,3)));
cal.set(Calendar.DAY_OF_YEAR,Integer.parseInt(dateStr.subString(4)));
Date parsedDate = cal.getTime();
---- EDIT ----
Thanks for Alex for providing the best answer:
Date myDate = new SimpleDateFormat("yyyyD").parse("2009218")
Another format is CYYDDDD I wrote this function in Java
public static int convertToJulian(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
String syear = String.format("%04d",year).substring(2);
int century = Integer.parseInt(String.valueOf(((year / 100)+1)).substring(1));
int julian = Integer.parseInt(String.format("%d%s%03d",century,syear,calendar.get(Calendar.DAY_OF_YEAR)));
return julian;
}