Fixed length of month and day in date format? - java

Is there any way to format Date object to made fixed length of Day and Month in order to have good alignment in a column?
For example:
15 May 2010
10 January 2010
Instead of
15 May 2010
10 January 2010
Thanks!

Have a look at the java.util.Formatter class whose format method is the same as String.format(...) and similar to System.out.printf.
For example:
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class FormatDateCalendar {
public static final String FORMAT_STRING = "%1$-3td %1$-9tB %1$tY";
public static void main(String[] args) {
Calendar c1 = new GregorianCalendar(2011, Calendar.FEBRUARY, 3);
Calendar c2 = new GregorianCalendar(2010, Calendar.MAY, 15);
Date today = new Date();
System.out.printf(FORMAT_STRING + "%n", c1);
System.out.printf(FORMAT_STRING + "%n", c2);
System.out.printf(FORMAT_STRING + "%n", today);
}
}

Related

DateTime addmonths method is adding 1 extra day

I am currently working on an issue where DateTime.addMonths(iStartDateH, durationAsInt) is adding an extra day. It uses GeorgianCalendar internally.
We are using Java 5 currently in this project
Eg: For 24 months
ExpirationDate=DateTime.addMonths(currentDate, 24)
CurrentDate= 01/02/2021 (dd/mm/yyyy format)
ExpirationDate= 02/02/2023
public static ErrorCode addMonths(DateHolder dateH, int numMonths) {
try {
Calendar c = new GregorianCalendar();
c.setTime(dateH.value);
c.add(Calendar.MONTH, numMonths);
dateH.value = c.getTime();
return ErrorCode.successCN;
}
catch (Exception e) {
IlMessage msg = new IlMessage(Msg.exceptionCaughtCN, e);
IlSession.getSession().getMessageStack().push(msg);
return ErrorCode.errorCN;
}
}
I tried checking the complete date/time difference and its coming as 730.773935185185185
Please help with the same.
I am using Java 8 and I tried the code below and it worked just fine for me (for testing purposes I set the date as Feb 1 as from your example.
public static void main(String...pStrings) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate currentDate = LocalDate.of(2021, 2, 1); //LocalDate.now();
System.out.println("Original Date -" +currentDate.format(formatter));
LocalDate newDate = currentDate.plusMonths(24);
System.out.println("updated date - " + newDate.format(formatter));
}
I received the output: -
Original Date -01/02/2021
updated date - 01/02/2023
Note that m is for minutes. For a month, you need to use M.
The implementation of your class, DateHolder seems to have a problem. There is no such problem with java.util date-time API for this requirement.
Demo:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class Main {
public static void main(String[] args) throws ParseException {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).parse("01/02/2021"));
System.out.println(calendar.getTime());
int numMonths = 24;
calendar.add(Calendar.MONTH, numMonths);
System.out.println(calendar.getTime());
}
}
Output:
Mon Feb 01 00:00:00 GMT 2021
Wed Feb 01 00:00:00 GMT 2023

DST Handling In Java 5 For Corner Dates

According to the following website daylight saving time in UK starts from 27 Mar-2016 1:00:00 (24 hours time format)
http://www.timeanddate.com/time/change/uk/london?year=2016
Then why the following code prints false?
import java.text.ParseException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class BSTGMTDetector
{
public static void main(String[] args) throws ParseException
{
TimeZone def = TimeZone.getTimeZone("Europe/London");
Calendar cal = new GregorianCalendar();
cal.clear();
cal.set(2016, 2, 27,2,01,01);
boolean isInDaylight = def.inDaylightTime(cal.getTime());
System.out.println("Date[" + cal.getTime() + "] is in DST[" + isInDaylight + "]");
}
}
Output : Date[Sun Mar 27 02:01:01 IST 2016] is in DST[false]
It seems that your system maintain IST.When you define,
Calendar cal = new GregorianCalendar();
Timezone for cal variable is IST.So,when call
def.inDaylightTime(cal.getTime());
Date is passed to this method in IST timezone.So,DST start for UK at 27th MAR,06:30:00 IST.
Hence.your O/P behaviour.

Unexpected Behavior using Dates in Java

I have the following code:
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import org.junit.Test;
public class DateCalendarTest {
#Test
public void test1() {
private final DateFormat df1 = new SimpleDateFormat("MM/dd/yyyy");
String batchdt = "2015/09/23";
System.out.println("Date & Calendar Test: " + batchdt);
Calendar cal = Calendar.getInstance();
Date date1 = df1.parse(batchdt);
cal.setTime(date1);
System.out.println("Date & Calendar Test: " + cal.getTime());
}
}
Output:
Date Calendar Test: 2015/09/23
Date Calendar Test: Mon Nov 09 00:00:00 EST 190
Can someone please explain why this behaves in this manner?
Your intended data doesn't match the format specified in your SimpleDateFormat. However, by default, it is lenient in how it parses the data. For example, September 31st would be interpreted as October 1st.
Here, it's interpreted as day 9 of month 2,015 of year 23. 2,015 months is 167 years, 11 months, which when added to the year 23 yields the year 190. In this case, it is very lenient.
The output format is what is expected when printing out a Date directly.

How do I convert a java.sql.Date object into a GregorianCalendar?

I thought I'd be able to create a GregorianCalendar using the constructor that takes the year, month, and day, but I can't reliably get those fields from an instance of the java.sql.Date class. The methods that get those values from java.sql.Date are deprecated, and the following code shows why they can't be used:
import java.sql.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class DateTester {
public static void main(String[] args) {
Date date = Date.valueOf("2011-12-25");
System.out.println("Year: " + date.getYear());
System.out.println("Month: " + date.getMonth());
System.out.println("Day: " + date.getDate());
System.out.println(date);
Calendar cal = new GregorianCalendar(date.getYear(), date.getMonth(), date.getDate());
System.out.println(cal.getTime());
}
}
Here's the output, showing that the month and year are not returned correctly from the deprecated getYear() and getMonth() methods of Date:
Year: 111
Month: 11
Day: 25
2011-12-25
Thu Dec 25 00:00:00 EST 111
Since I can't use the constructor that I tried above, and there's no GregorianCalendar constructor that just takes a Date, how can I convert a java.sql.Date object into a GregorianCalendar?
You have to do this in two steps. First create a GregorianCalendar using the default constructor, then set the date using the (confusingly named) setTime method.
import java.sql.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class DateTester {
public static void main(String[] args) {
Date date = Date.valueOf("2011-12-25");
System.out.println(date);
Calendar cal = new GregorianCalendar();
cal.setTime(date);
System.out.println(cal.getTime());
}
}
Here's the output:
2011-12-25
Sun Dec 25 00:00:00 EST 2011
I'm going from memory, but have you tried
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(rs.getDate());
Try this.
import java.sql.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class DateTester {
public static void main(String[] args) {
Date date = Date.valueOf("2011-12-25");
System.out.println(date);
Calendar cal = new GregorianCalendar();
cal.setTime(date);
System.out.println(cal.getTime());
}
}
Use setTimeInMillis():
java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(date.getTime());
I think this is the simplest way.

Java GregorianCalendar What am I doing wrong? Wrong date?

Hello I have a problem with GregorianCalendar.
What is wrong in there?
How outcome is 2010/6/1 and not 2010/05/31?
package test;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Main {
public static void main(String[] args) {
Calendar cal = new GregorianCalendar(2010, 5, 31);
System.out.println(cal.get(Calendar.YEAR) + "/" + cal.get(Calendar.MONTH) + "/" + cal.get(Calendar.DAY_OF_MONTH));
}
}
Java counts months from 0, so 5 is June. It's always safer to use the constants. So I would write:
Calendar cal = new GregorianCalendar(2010, Calendar.MAY, 31);
The same applies to your calendar print out. If you do cal.get(Calendar.MONTH) you get 6 meaning JULY.
This is because month number is zero-based, so you are trying to set 31st of June, but June is only 30 days, so it gets converted to 1st of July.
Toadd to what the above answers, since there is no 31st day in June the Calendar promotes it to the next valid day because Calendar.setLenient is true by default.

Categories

Resources