[Source | Java] String to Date & Date to Timestamp

Java에서 String을 Timestamp로 변환하는 코드를 정리한다.


작성일 : 2021-06-18


// Define
String dateStr = "2021-06-18 08:32:57";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

1> String to Date

Date date = dateFormat.parse(dateStr);
System.out.println(date);
System.out.println(dateFormat.format(date));

2> Date to Timestamp

Long timestamp = date.getTime();
System.out.println(timestamp);

3> Timestamp to String

[Link – Timestamp to Date & Date to String]

[Source | Java] InputStream to String

Java에서 InputStream객체를 String으로 변환하는 코드를 정리한다.


작성일 : 2021-04-23


// Define
InputStream is = {SomeData};

1> BufferedReader 객체사용

InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringBuffer sb = new StringBuffer();
String line;
while((line = br.readLine()) != null) {
	sb.append(line);
	sb.append('\r');
}
br.close();
// Result
String str = sb.toString();

2> StringBuffer 객체사용

StringBuffer sb = new StringBuffer();
byte[] b = new byte[4096];
int length;
while((length = is.read(b)) != -1) {
	sb.append(new String(b, 0, length));
}
// Result
String str = sb.toString();

3>  ByteArrayOutputStream 객체사용

ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[4096];
int length;
while((length = is.read(b)) != -1) {
	baos.write(b, 0, length);
}
baos.flush();
// Result
String str = new String(baos.toByteArray());

[Source | Nodejs] 이메일 발송

Nodejs에서 이메일 발송코드를 정리한다.


작성일 : 2021-04-22
nodeJs Ver : v16.13.1


1> nodemailer 설치

npm install nodemailer

2> 코드작성

let transporter = nodemailer.createTransport({
   host: "OPENDOCS_SMTP_HOST",   // SMTP Host
   port: 465,
   secure: true,                 // true for 465, false for other ports
   auth: {
      user: "OPENDOCS_USERID",   // User ID
      pass: "OPENDOCS_USERPW"    // User Password
   }
});

let info = await transporter.sendMail({
   from: '"OPENDOCS" <corp@opendocs.co.kr>',     // Sender Address
   to: "opendocs@naver.com, opendocs@kakao.com", // Receiver List
   subject: "Hello ✔", // Subject line
   text: "Hello world?", // plain text body
   html: "<b>Hello world?</b>" // html body
});