CompSci / Programming Basics
About 1 min
data class FooBar(
val a: String = ""
val b: String = ""
val c: Boolean = false,
val d: Boolean = false,
val e: String = ""
) {
class Builder {
private var bA: String = ""; fun a(block: () -> String) { bA = block() }
private var bB: String = ""; fun b(block: () -> String) { bB = block() }
private var bC: String = ""; fun c(block: () -> String) { bC = block() }
private var bD: String = ""; fun d(block: () -> String) { bD = block() }
private var bE: String = ""; fun e(block: () -> String) { bE = block() }
fun build(): FooBar = FooBar(bA, bB, bC, bD, bE)
}
companion object {
inline fun builder(block: Builder.() -> Unit): FooBar = Builder().apply(block).build()
}
}
@tab:active 기본
status=warn
name=DemoLog
appenders = stdout
# 콘솔 출력 설정
appender.stdout.type = Console
appender.stdout.name = STDOUT
appender.stdout.layout.type = PatternLayout
appender.stdout.layout.pattern = [%t] %-5p : %c.%M(%F:%L) %-80m %n
appender.stdout.filter.threshold.type = ThresholdFilter
appender.stdout.filter.threshold.level = debug
rootLogger.level = debug
rootLogger.appenderRef.stdout.additivity = false
rootLogger.appenderRef.stdout.ref = STDOUT
Singleton 은 객체 생성을 제한시키는 목적외에도 지연 초기화(lazy initializaion) 목적도 있다.
public class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null)
instance = new Singleton();
return instance;
}
}
private const val TIME_STAMP_FORMAT = "EEEE. MMM d, yyyy - hh:mm:ss a"
private const val DATE_FORMAT = "yyyy-MM-dd"
fun Long.getTimeStamp(): String {
val date = Date(this)
val simpleDateFormat = SimpleDateFormat(TIME_STAMP_FORMAT, Locale.getDefault())
simpleDateFormat.timeZone = TimeZone.getDefault()
return simpleDateFormat.format(date)
}
fun Long.getYearMonthDay(): String {
val date = Date(this)
val simpleDateFormat = SimpleDateFormat(DATE_FORMAT, Locale.getDefault())
simpleDateFormat.timeZone = TimeZone.getDefault()
return simpleDateFormat.format(date)
}
@Throws(ParseException::class)
fun String.getDateUnixTime(): Long {
try {
val simpleDateFormat = SimpleDateFormat(DATE_FORMAT, Locale.getDefault())
simpleDateFormat.timeZone = TimeZone.getDefault()
return simpleDateFormat.parse(this)!!.time
} catch (e: ParseException) {
e.printStackTrace(0)
}
throw ParseException("Please Enter a valid date", 0)
}
val currentTime = System.currentTimeMillis()
println(currentTime.getTimeStamp())
// Sunday, September 20, 2020 - 10:48:26 AM
println(currentTime.getYearMonthDay())
// 2020-09-20
println("2020-09-20".getDateUnixTime())
// 1600549200000
If you use SLF4J (and possibly Logback) for logging, you are probably familiar with the following code:
val logger = LoggerFactory.getLogger(MyClass::class.java)