List of usage examples for java.util Calendar getInstance
public static Calendar getInstance()
From source file:UdpEchoClient.java
public static void main(String[] args) { InetAddress address;/*from ww w.j av a 2 s . c o m*/ try { address = InetAddress.getByName(args[0]); } catch (UnknownHostException host) { System.out.println(host); return; } DatagramPacket pack = new DatagramPacket(testString.getBytes(), testString.length(), address, 7); DatagramPacket incoming = new DatagramPacket(new byte[256], 256); DatagramSocket sock = null; try { Calendar start, end; sock = new DatagramSocket(); start = Calendar.getInstance(); sock.send(pack); sock.setSoTimeout(5000); sock.receive(incoming); end = Calendar.getInstance(); String reply = new String(incoming.getData()); reply = reply.substring(0, testString.length()); if (reply.equals(testString)) { System.out.println("Success"); System.out.println("Time = " + (end.getTime().getTime() - start.getTime().getTime()) + "mS"); } else System.out.println("Reply data did not match"); } catch (SocketException socke) { System.out.println(socke); } catch (IOException ioe) { System.out.println(ioe); } finally { sock.close(); } }
From source file:MainClass.java
public static void main(String[] pArgs) throws Exception { Calendar cal = Calendar.getInstance(); cal.set(2004, 2, 5, 10, 2, 2);/*w w w. ja v a 2 s . c om*/ System.out.println(DateUtils.round(cal.getTime(), DateUtils.SEMI_MONTH)); }
From source file:com.cjwagner.InfoSecPriv.ExtensionServer.java
public static void main(String[] args) { initializeLogStore();//www . ja v a 2s . c om get("/data", (request, response) -> { Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.add(Calendar.MINUTE, -5); Date tMinusDelta = cal.getTime(); Map<String, LoggerMessage> filtered = new HashMap<String, LoggerMessage>(); for (Map.Entry<String, LoggerMessage> entry : logStore.entrySet()) { String ip = entry.getKey(); LoggerMessage logmess = entry.getValue(); LoggerMessage logmessFiltered = new LoggerMessage(); logmessFiltered.setFirstLogTime(logmess.getFirstLogTime()); List<LogData> filteredData = new ArrayList<LogData>(); logmessFiltered.setLogs(filteredData); for (LogData data : logmess.getLogs()) { if (data.getDate().after(tMinusDelta)) { filteredData.add(data); } } filtered.put(ip, logmessFiltered); } ObjectMapper objMapper = new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, true); String jsonResponse = objMapper.writeValueAsString(filtered); System.out.println("Responded to query for recent data from IP: " + request.ip()); return jsonResponse; }); post("/LicenseRegistry", (request, response) -> { if (storeSize >= MAXSTORESIZE) { response.status(507);//insufficient storage return "Server storage full!"; } String ip = request.ip().replace(':', '_'); String json = request.body(); try { LoggerMessage logMess = LoggerMessage.fromJSON(json); logMess.setFirstLogTime(new Date()); LoggerMessage rec = logStore.get(ip); if (rec == null) { logStore.put(ip, logMess); rec = logMess; } else { rec.getLogs().addAll(logMess.getLogs()); } updateLogFile(ip, rec); storeSize += logMess.getLogs().size(); response.status(200); System.out.println("Recieved log data from IP: " + ip); return "LicenseKey:<c706cfe7-b748-4d75-98b5-c6b32ab789cb>"; } catch (JsonParseException jpe) { response.status(HTTP_BAD_REQUEST); System.out.println("Failed to parse log data from IP: " + ip); return jpe.getMessage(); } }); }
From source file:fitmon.Client.java
public static void main(String[] args) throws IOException, ClientProtocolException, NoSuchAlgorithmException, InvalidKeyException, SAXException, ParserConfigurationException { Food food = null;//from w ww . j a va 2s . co m DietAPI dApi = new DietAPI(); JDBCConnection jdbcCon = new JDBCConnection(); ArrayList<Food> foodItems = dApi.getFood(); Scanner scan = new Scanner(System.in); Calendar currentDate = Calendar.getInstance(); //Get the current date SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd"); //format it as per your requirement String dateNow = formatter.format(currentDate.getTime()); System.out.println("Now the date is :=> " + dateNow); for (int i = 0; i < foodItems.size(); i++) { food = foodItems.get(i); System.out.println("ID : " + food.getFoodID()); System.out.println("servingID : " + food.getServingID()); System.out.println("Name : " + food.getItemName()); System.out.println("category : " + food.getCategory()); System.out.println("Quantity : " + food.getQuantity()); System.out.println("calories : " + food.getCalories()); System.out.println("fat : " + food.getFat()); System.out.println("carbs : " + food.getCarbs()); System.out.println("protein : " + food.getProtein()); System.out.println("fiber : " + food.getFiber()); System.out.println("sodium : " + food.getSodium()); System.out.println("sugar : " + food.getSugar()); System.out.println("cholesterol : " + food.getCholesterol()); System.out.println( "------------------------------------------------------------------------------------------------"); } System.out.println("Choose a meal......"); String mealType = scan.next(); System.out.println("Choose an item......"); String servingID = scan.next(); for (int j = 0; j < foodItems.size(); j++) { if (foodItems.get(j).getServingID() == null ? servingID == null : foodItems.get(j).getServingID().equals(servingID)) { food = foodItems.get(j); break; } } Diet diet = new CustomizedDiet(); diet.createDiet(food, mealType, dateNow); }
From source file:MainClass.java
public static void main(String argv[]) throws Exception { Calendar cc1 = Calendar.getInstance(); Calendar cc2 = Calendar.getInstance(); cc1.add(Calendar.DAY_OF_MONTH, 10); DateDiff(cc1.getTime(), cc2.getTime()); java.util.Date d1 = makeDate("10/10/2000"); java.util.Date d2 = makeDate("10/18/2000"); DateDiff(d1, d2);// www. jav a 2 s . c om java.util.Date d3 = makeDate("1/1/2000"); java.util.Date d4 = makeDate("12/31/2000"); int diff34 = diff(d3, d4); System.out.println("diff34=" + diff34); }
From source file:com.apress.prospringintegration.social.twitter.TwitterOutbound.java
public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "/spring/twitter/twitter-outbound.xml", TwitterOutbound.class); MessageChannel input = context.getBean("twitterOutbound", MessageChannel.class); Message<String> message = MessageBuilder .withPayload("Only can send message once " + Calendar.getInstance().getTimeInMillis()).build(); input.send(message);/*w w w. j a v a2s . c om*/ context.stop(); }
From source file:org.eclipse.swt.snippets.Snippet179.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Snippet 179"); shell.setLayout(new GridLayout()); final Text text = new Text(shell, SWT.BORDER); text.setText("YYYY/MM/DD"); final Calendar calendar = Calendar.getInstance(); text.addListener(SWT.Verify, new Listener() { boolean ignore; @Override/*from w ww.ja v a 2s. co m*/ public void handleEvent(Event e) { if (ignore) return; e.doit = false; StringBuilder buffer = new StringBuilder(e.text); char[] chars = new char[buffer.length()]; buffer.getChars(0, chars.length, chars, 0); if (e.character == '\b') { for (int i = e.start; i < e.end; i++) { switch (i) { case 0: /* [Y]YYY */ case 1: /* Y[Y]YY */ case 2: /* YY[Y]Y */ case 3: /* YYY[Y] */ { buffer.append('Y'); break; } case 5: /* [M]M*/ case 6: /* M[M] */ { buffer.append('M'); break; } case 8: /* [D]D */ case 9: /* D[D] */ { buffer.append('D'); break; } case 4: /* YYYY[/]MM */ case 7: /* MM[/]DD */ { buffer.append('/'); break; } default: return; } } text.setSelection(e.start, e.start + buffer.length()); ignore = true; text.insert(buffer.toString()); ignore = false; text.setSelection(e.start, e.start); return; } int start = e.start; if (start > 9) return; int index = 0; for (int i = 0; i < chars.length; i++) { if (start + index == 4 || start + index == 7) { if (chars[i] == '/') { index++; continue; } buffer.insert(index++, '/'); } if (chars[i] < '0' || '9' < chars[i]) return; if (start + index == 5 && '1' < chars[i]) return; /* [M]M */ if (start + index == 8 && '3' < chars[i]) return; /* [D]D */ index++; } String newText = buffer.toString(); int length = newText.length(); StringBuilder date = new StringBuilder(text.getText()); date.replace(e.start, e.start + length, newText); calendar.set(Calendar.YEAR, 1901); calendar.set(Calendar.MONTH, Calendar.JANUARY); calendar.set(Calendar.DATE, 1); String yyyy = date.substring(0, 4); if (yyyy.indexOf('Y') == -1) { int year = Integer.parseInt(yyyy); calendar.set(Calendar.YEAR, year); } String mm = date.substring(5, 7); if (mm.indexOf('M') == -1) { int month = Integer.parseInt(mm) - 1; int maxMonth = calendar.getActualMaximum(Calendar.MONTH); if (0 > month || month > maxMonth) return; calendar.set(Calendar.MONTH, month); } String dd = date.substring(8, 10); if (dd.indexOf('D') == -1) { int day = Integer.parseInt(dd); int maxDay = calendar.getActualMaximum(Calendar.DATE); if (1 > day || day > maxDay) return; calendar.set(Calendar.DATE, day); } else { if (calendar.get(Calendar.MONTH) == Calendar.FEBRUARY) { char firstChar = date.charAt(8); if (firstChar != 'D' && '2' < firstChar) return; } } text.setSelection(e.start, e.start + length); ignore = true; text.insert(newText); ignore = false; } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:com.apress.prospringintegration.springenterprise.stocks.runner.MainHibernateJPA.java
public static void main(String[] args) { GenericApplicationContext context = new AnnotationConfigApplicationContext( "com.apress.prospringintegration.springenterprise.stocks.dao.jpa"); StockDao stockDao = context.getBean("jpaStockDao", StockDao.class); Stock stock = new Stock("ORAC", "JPAMAIN0001", "QQQQ", 120.0f, 1100, Calendar.getInstance().getTime()); stockDao.insert(stock);//from w ww .java2 s . c om List<Stock> stocks = stockDao.findAvailableStockBySymbol("ORAC"); if (stocks != null && stocks.size() > 0) { stock = stocks.get(0); System.out.println("Stock Symbol :" + stock.getSymbol()); System.out.println("Inventory Code :" + stock.getInventoryCode()); System.out.println("purchased price:" + stock.getSharePrice()); System.out.println("Exchange ID:" + stock.getExchangeId()); System.out.println("Quantity Available :" + stock.getQuantityAvailable()); } }
From source file:com.apress.prospringintegration.springenterprise.stocks.runner.MainHibernate.java
public static void main(String[] args) { GenericApplicationContext context = new AnnotationConfigApplicationContext( "com.apress.prospringintegration.springenterprise.stocks.dao.hibernate"); StockDao stockDao = context.getBean("hibernateStockDao", StockDao.class); Stock stock = new Stock("ORAC", "HIBERNATEMAIN0001", "QQQQ", 120.0f, 1100, Calendar.getInstance().getTime()); stockDao.insert(stock);// w ww . j a v a 2 s . c o m stock = stockDao.findByInventoryCode("HIBERNATEMAIN0001"); if (stock != null) { System.out.println("Stock Symbol :" + stock.getSymbol()); System.out.println("Inventory Code :" + stock.getInventoryCode()); System.out.println("purchased price:" + stock.getSharePrice()); System.out.println("Exchange ID:" + stock.getExchangeId()); System.out.println("Quantity Available :" + stock.getQuantityAvailable()); } }
From source file:com.apress.prospringintegration.springenterprise.stocks.runner.MainHibernateEntityManager.java
public static void main(String[] args) { GenericApplicationContext context = new AnnotationConfigApplicationContext( "com.apress.prospringintegration.springenterprise.stocks.dao.jpaandentitymanagers"); StockDao stockDao = context.getBean("jpaStockDao", StockDao.class); Stock stock = new Stock("ORAC", "JPAMAIN0001", "QQQQ", 120.0f, 1100, Calendar.getInstance().getTime()); stockDao.insert(stock);// w ww .jav a 2 s . co m List<Stock> stocks = stockDao.findAvailableStockBySymbol("ORAC"); if (stocks != null && stocks.size() > 0) { stock = stocks.get(0); System.out.println("Stock Symbol :" + stock.getSymbol()); System.out.println("Inventory Code :" + stock.getInventoryCode()); System.out.println("purchased price:" + stock.getSharePrice()); System.out.println("Exchange ID:" + stock.getExchangeId()); System.out.println("Quantity Available :" + stock.getQuantityAvailable()); } }