Programming Archives

Printing XYZ in Java using Asterisks

Posted on March 25, 2014 at 23:23 Java No Comments

Assumption is that dimension should always be an odd number (you have to be able to have immediately understood why, else please stop reading this this instant). It’s accomplished using brute force, because I just wanted to see it working and prove to myself that I haven’t gone totally rusty yet.

package letter;

public class LetterGenerator {
   public static void main(String[] args) {
      int dim = 7;

      for(int i = 0; i < dim; i++) {
         for(int j = 0; j < dim; j++) {
            if(i == j || i + j == dim - 1) {
               System.out.print("*");
            } else {
               System.out.print(" ");
            }
         }

         System.out.println();
      }

      System.out.println();

      for(int i = 0; i < dim; i++) {
         for(int j = 0; j < dim; j++) {
            if(((i == j || i + j == dim - 1) && i < dim / 2 + 1) || (i > dim / 2 && j == dim / 2)) {
               System.out.print("*");
            } else {
               System.out.print(" ");
            }
         }

         System.out.println();
      }

      System.out.println();

      for(int i = 0; i < dim; i++) {
         for(int j = 0; j < dim; j++) {
            if(i == 0 || i + j == dim - 1 || i == dim - 1) {
               System.out.print("*");
            } else {
               System.out.print(" ");
            }
         }

         System.out.println();
      }
   }
}

Generating Dates Between Start and End in Java

Posted on August 11, 2012 at 13:14 Java No Comments

Yesterday I had to generate a list of dates given two inputs. Having no Internet connection yet, I eagerly searched for help through my Android phone. Stack Overflow would’ve instantly helped me if I didn’t leave immediately after reading “Joda Time”. The thought of not needing some library for such an ideally simple task was simply there. Then there was this, which compiled but never ran. :faint:

In my case, the checking that the end date should be greater than the start date was already handled somewhere else so I already skipped it. Also, instead of actual Dates, I only needed Strings (but I do intend to convert this into a generic utility class). So, yeah, here you go:

final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd");
Calendar startDate = Calendar.getInstance();
Calendar endDate = Calendar.getInstance();

try {
   startDate.setTime(DATE_FORMAT.parse("2012/08/01"));
   endDate.setTime(DATE_FORMAT.parse("2012/08/10"));

   ArrayList<String> includedDates = new ArrayList<String>();

   while(!startDate.equals(endDate)) {
      String date = DATE_FORMAT.format(startDate.getTime());
      includedDates.add(date);
      startDate.add(Calendar.DATE, 1);
   } includedDates.add(DATE_FORMAT.format(endDate.getTime()));

   for(String date : includedDates) {
      System.out.println(date);
   }
} catch(Exception e) {}

Today I checked back on the supposedly second working solution I cited above, copy-pasted the code, and successfully ran it. :wtf: