Java2014.08.19 14:01




자바 버젼 Velocity 샘플입니다.

jar는 두가지를 참조했습니다. velocity-1.7.jar, velocity-1.7-dep.jar

 

import java.io.OutputStreamWriter;
import java.util.ArrayList;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

public class VelocityExMain {

	public static void main(String[] args) {
		Example("templateHTML.vm");
	}

	public static void Example(String templateFile) {
		try {

			try {
				Velocity.init();
			} catch (Exception e) {
				System.out.println("Problem initializing Velocity : " + e);
				return;
			}

			VelocityContext context = new VelocityContext();
			context.put("list", getNames());
			context.put("newPara", "PSJ");

			Template template = null;

			try {
				template = Velocity.getTemplate(templateFile);
			} catch (ResourceNotFoundException rnfe) {
				System.out.println("error : cannot find template "
						+ templateFile);
			} catch (ParseErrorException pee) {
				System.out.println("Syntax error in template " + templateFile
						+ ":" + pee);
			}

			// BufferedWriter writer = writer = new BufferedWriter(
			// new OutputStreamWriter(System.out));

			FileWriter writer = new FileWriter("newHTML.txt");

			if (template != null)
				template.merge(context, writer);

			writer.flush();
			writer.close();
		} catch (Exception e) {
			System.out.println(e);
		}
	}

	public static ArrayList getNames() {
		ArrayList list = new ArrayList();

		list.add("ArrayList element 1");
		list.add("ArrayList element 2");
		list.add("ArrayList element 3");
		list.add("ArrayList element 4");

		return list;
	}
}
#foreach( $name in $list )
    $name is great!
#end

#set( $condition = true)

#if ($condition)
    The condition is true!
#else
    The condition is false!
#end

$newPara is good man.
신고




Posted by 금붕어70
Java2014.08.19 10:07




안드로이드 앱을 개발하다 보면 Listview 아이템들의 생성 일자별로 구분자를 넣어서 Today(오늘), Yesterday(어제), This week(이번 주), Last week(지난 주), This Month(이번 주),... 항목들을 계산할 필요가 있었습니다. 이러한 날자 계산은 생각보다 상당히 까다롭습니다.

그래서 찾은 라이브러리가 Joda(http://www.joda.org/joda-time/) 입니다. 시간과 날자 계산은 탁월한데 정확한 사용방법을 모르면...

Joda 라이브러리를 사용하여 간단한 샘플을 만들어 보았습니다.

 

package com.nalsil.JodaDate;

import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.joda.time.Months;
import org.joda.time.Weeks;

public class JodaDateTime {

	public static void main(String[] args) {

		System.out.println(String.format("Now: %s", DateTime.now().toString()));

		for (int idxDay = 1; idxDay <= DateTime.now().dayOfMonth().withMinimumValue().minusDays(1).getDayOfMonth(); idxDay++) {
			displayMWD(DateTime.now().dayOfMonth().withMinimumValue().minusDays(1).getYear(), DateTime.now().dayOfMonth()
					.withMinimumValue().minusDays(1).getMonthOfYear(), idxDay);
		}

		for (int idxDay = 1; idxDay <= DateTime.now().getDayOfMonth(); idxDay++) {
			displayMWD(DateTime.now().dayOfMonth().withMinimumValue().minusDays(1).getYear(), DateTime.now().getMonthOfYear(), idxDay);
		}
	}

	private static void displayMWD(int year, int month, int day) {

		DateTime now = DateTime.now();
		DateTime start = new DateTime(year, month, day, 0, 0, 0);
		int nDays = Days.daysBetween(new LocalDate(start), new LocalDate(now)).getDays();
		int nWeeks = Weeks.weeksBetween(start.dayOfWeek().withMinimumValue().minusDays(1), now).getWeeks();
		int nMonths = Months.monthsBetween(start.dayOfMonth().withMinimumValue().minusDays(1), now).getMonths();
		System.out.println(String.format("%s : days=>%d, weeks=>%d, months=>%d", start.toString(), nDays, nWeeks, nMonths));
	}

}
신고




Posted by 금붕어70
Java2014.07.09 20:39




http://stackoverflow.com/questions/8422842/in-java-syntax-class-extends-something

There are a few confusing answers here so I will try and clear this up. You define a generic as such:

public class Foo<T> {
    private T t;
    public void setValue(T t) {
        this.t = t;
    }
    public T getValue() {
        return t;
    }
}

If you want a generic on Foo to always extend a class Bar you would declare it as such:

public class Foo<T extends Bar> {
    private T t;
    public void setValue(T t) {
        this.t = t;
    }
    public T getValue() {
        return t;
    }
}

The ? is used when you declare a variable.

Foo<? extends Bar>foo = getFoo();

OR

DoSomething(List<? extends Bar> listOfBarObjects) {
    //internals
}
신고




Posted by 금붕어70