Monday, May 30, 2022

How to create MS Word file using Java

Use Maven to add dependency:


  <dependency>

<groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency>


public class CreateDocumentSimple { public static void main(String[] args) throws IOException { String fileName = "c:\\test\\hello.docx"; try (XWPFDocument doc = new XWPFDocument()) { // create a paragraph XWPFParagraph p1 = doc.createParagraph(); p1.setAlignment(ParagraphAlignment.CENTER); // set font XWPFRun r1 = p1.createRun(); r1.setBold(true); r1.setItalic(true); r1.setFontSize(22); r1.setFontFamily("New Roman"); r1.setText("I am first paragraph."); // save it to .docx file try (FileOutputStream out = new FileOutputStream(fileName)) { doc.write(out); } } } }

Tuesday, May 3, 2022

Javascript display Date with format

module.exports={
getDisplayDate,
}

function getDisplayDate(dateObj){
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
let monthNumber=dateObj.getMonth()
return dateObj.getDate()+"/"+months[monthNumber]+"/"+dateObj.getFullYear();
}

var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
var today  = new Date();

console.log(today.toLocaleDateString("en-US")); // 9/17/2016
console.log(today.toLocaleDateString("en-US", options)); // Saturday, September 17, 2016
console.log(today.toLocaleDateString("hi-IN", options));

date. toLocaleDateString("en-US", options)



Option # 2

Use the date.format library:

var dateFormat = require('dateformat');
var now = new Date();
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");


Using Moment.js

var moment = require('moment'); // require
moment().format();

For Node.js


var format = require('date-format');
format.asString(); // defaults to ISO8601 format and current date
format.asString(new Date()); // defaults to ISO8601 format
format.asString('hh:mm:ss.SSS', new Date()); 

Sunday, May 1, 2022

getting started with PM2

a)  Install:

 sudo npm install pm2 -g

b) Start Node api in cluster mode

pm2 --name <apiname> start index.js -i 2

c) Save Pm2 job

pm2 save


d) auto start at reboot

pm2 startup


e) reload without downtime

pm2 reload all