Example usage for java.util Calendar getTime

List of usage examples for java.util Calendar getTime

Introduction

In this page you can find the example usage for java.util Calendar getTime.

Prototype

public final Date getTime() 

Source Link

Document

Returns a Date object representing this Calendar's time value (millisecond offset from the Epoch").

Usage

From source file:MainClass.java

public static void main(String[] args) {

    // get a calendar using the default time zone and locale.
    Calendar calendar = Calendar.getInstance();

    // set Date portion to January 1, 1970
    calendar.set(Calendar.YEAR, 1970);
    calendar.set(Calendar.MONTH, Calendar.JANUARY);
    calendar.set(Calendar.DATE, 1);

    // normalize the object
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    java.sql.Date javaSqlDate = new java.sql.Date(calendar.getTime().getTime());
}

From source file:com.isoftstone.proxy.Program.java

public static void main(String[] args) throws IOException {
    int insertIntervalTime = Integer.valueOf(Config.getValue("proxy_insert_minutes"));
    int checkIntervalTime = Integer.valueOf(Config.getValue("proxy_check_minute"));
    SchedulerFactory sf = new StdSchedulerFactory();
    Scheduler sched;//from w  w  w .j  a  va  2s  .c  o  m
    try {
        sched = sf.getScheduler();

        // ????
        JobDetail parseProxyJob = JobBuilder.newJob(ParseProxyJob.class).withIdentity("parseProxyJob", "Group")
                .build();

        SimpleTrigger parseProxyTrigger = (SimpleTrigger) TriggerBuilder
                .newTrigger().withIdentity("parseProxyJob", "Group").withSchedule(SimpleScheduleBuilder
                        .simpleSchedule().withIntervalInMinutes(insertIntervalTime).repeatForever())
                .startAt(new Date()).build();

        sched.scheduleJob(parseProxyJob, parseProxyTrigger);

        // ??
        JobDetail checkProxyJob = JobBuilder.newJob(CheckProxyJob.class).withIdentity("checkProxyJob", "Group")
                .build();

        /**
         * 10s?.
         */
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, 10);

        SimpleTrigger checkProxyTrigger = (SimpleTrigger) TriggerBuilder.newTrigger()
                .withIdentity("checkProxyJob", "Group").withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInMinutes(checkIntervalTime).repeatForever())
                .startAt(calendar.getTime()).build();

        sched.scheduleJob(checkProxyJob, checkProxyTrigger);

        sched.start();

        String uriHost = Config.getValue("http_server_host");
        URI baseUri = UriBuilder.fromUri(uriHost).port(9998).build();
        ResourceConfig config = new ResourceConfig(HttpProxyServer.class);
        HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, config);

    } catch (SchedulerException e) {
        LOG.error("SchedulerException", e);
    }
}

From source file:info.usbo.skypetwitter.Run.java

public static void main(String[] args) {
    System.out.println("Working Directory = " + System.getProperty("user.dir"));
    Properties props;// w  w  w  . j  a v a2s .  c o  m
    props = (Properties) System.getProperties().clone();
    //loadProperties(props, "D:\\Data\\Java\\classpath\\twitter.props");
    work_dir = System.getProperty("user.dir");
    loadProperties(props, work_dir + "\\twitter.props");
    twitter_user_id = props.getProperty("twitter.user");
    twitter_timeout = Integer.parseInt(props.getProperty("twitter.timeout"));
    System.out.println("Twitter user: " + twitter_user_id);
    System.out.println("Twitter timeout: " + twitter_timeout);
    if ("".equals(twitter_user_id)) {
        return;
    }
    if (load_file() == 0) {
        System.out.println("File not found");
        return;
    }
    while (true) {
        bChanged = 0;
        /*
        create_id();
        Chat ch = Chat.getInstance(chat_group_id);
        ch.send("!");
        */
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
        System.out.println("Looking at " + sdf.format(Calendar.getInstance().getTime()));
        Chat ch = Chat.getInstance(chat_group_id);
        Twitter twitter = new TwitterFactory().getInstance();
        try {
            List<Status> statuses;
            statuses = twitter.getUserTimeline(twitter_user_id);
            //System.out.println("Showing @" + twitter_user_id + "'s user timeline.");
            String sText;
            for (Status status : statuses) {
                //System.out.println(status.getId());
                Date d = status.getCreatedAt();
                //   ?
                Calendar cal = Calendar.getInstance();
                cal.setTime(d);
                cal.add(Calendar.HOUR_OF_DAY, 7);
                d = cal.getTime();
                sText = "@" + status.getUser().getScreenName() + "  " + sdf.format(d)
                        + " ( https://twitter.com/" + twitter_user_id + "/status/" + status.getId() + " ): \r\n"
                        + status.getText() + "\r\n***";
                for (URLEntity e : status.getURLEntities()) {
                    sText = sText.replaceAll(e.getURL(), e.getExpandedURL());
                }
                for (MediaEntity e : status.getMediaEntities()) {
                    sText = sText.replaceAll(e.getURL(), e.getMediaURL());
                }
                if (twitter_ids.indexOf(status.getId()) == -1) {
                    System.out.println(sText);
                    ch.send(sText);
                    twitter_ids.add(status.getId());
                    bChanged = 1;
                }
            }
        } catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to get timeline: " + te.getMessage());
            //System.exit(-1);
        } catch (SkypeException ex) {
            ex.printStackTrace();
            System.out.println("Failed to send message: " + ex.getMessage());
        }

        // VK
        try {
            vk();
            for (VK v : vk) {
                if (vk_ids.indexOf(v.getId()) == -1) {
                    Date d = v.getDate();
                    //   ?
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(d);
                    cal.add(Calendar.HOUR_OF_DAY, 7);
                    d = cal.getTime();
                    String sText = "@Depersonilized (VK)  " + sdf.format(d)
                            + " ( http://vk.com/Depersonilized?w=wall-0_" + v.getId() + " ): \r\n"
                            + v.getText();
                    if (!"".equals(v.getAttachment())) {
                        sText += "\r\n" + v.getAttachment();
                    }
                    sText += "\r\n***";
                    System.out.println(sText);
                    ch.send(sText);
                    vk_ids.add(v.getId());
                    bChanged = 1;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println("Failed to get vk: " + e.getMessage());
            //System.exit(-1);
        } catch (SkypeException ex) {
            ex.printStackTrace();
            System.out.println("Failed to send message: " + ex.getMessage());
        }
        if (bChanged == 1) {
            save_file();
        }
        try {
            Thread.sleep(1000 * 60 * twitter_timeout);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
            System.out.println("Failed to sleep: " + ex.getMessage());
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
    Date workDate = simpleDateFormat1.parse("2011-11-27");

    Calendar workCalendar = Calendar.getInstance();
    workCalendar.setTime(workDate);/*from   ww  w .j  ava  2s  .c om*/
    SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("HH:mm:ss");
    Calendar time = Calendar.getInstance();
    time.setTime(simpleDateFormat2.parse("06:00:00"));
    workCalendar.set(Calendar.HOUR_OF_DAY, time.get(Calendar.HOUR_OF_DAY));
    workCalendar.set(Calendar.MINUTE, time.get(Calendar.MINUTE));
    workCalendar.set(Calendar.SECOND, time.get(Calendar.SECOND));

    Date newWorkDate = workCalendar.getTime();

    SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    System.out.println(simpleDateFormat3.format(newWorkDate));
}

From source file:com.appeligo.epg.EpgIndexer.java

public static void main(String[] args) throws Exception {
    HessianProxyFactory factory = new HessianProxyFactory();
    EPGProvider epg = (EPGProvider) factory.create(EPGProvider.class, "http://localhost/epg/channel.epg");
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.HOUR, -5);//from   w  ww  .  j  av a  2 s.  c  om
    cal.set(Calendar.DATE, 1);
    String[] lineups = new String[] { "SDTW-C", "P-C", "P-DC", "P-S", "M-C", "M-DC", "M-S", "E-C", "E-DC",
            "E-S", "H-C", "H-DC", "H-S" };
    List<String> ids = epg.getModifiedProgramIds(cal.getTime());
    int count = 0;
    long average = 0;
    int counter = 0;
    int added = 0;
    while (count < ids.size()) {
        System.err.println("in loop: " + counter + ", " + count + "," + ids.size());
        int subsetSize = (ids.size() < 100 ? ids.size() : 100);
        counter++;
        if (count % 1000 == 0) {
            log.debug("Index programs into the Lucene Index. Current have processed " + count
                    + " programs out of " + ids.size());
        }
        int endIndex = (count + subsetSize > ids.size() ? ids.size() : count + subsetSize);
        List<String> subset = ids.subList(count, endIndex);
        count += subsetSize;

        long time = System.currentTimeMillis();
        HashMap<String, List<ScheduledProgram>> schedules = new HashMap<String, List<ScheduledProgram>>();
        for (String lineup : lineups) {
            ScheduledProgram[] programs = epg.getNextShowingList(lineup, subset);
            for (ScheduledProgram program : programs) {
                if (program != null) {
                    List<ScheduledProgram> schedule = schedules.get(program.getProgramId());
                    if (schedule == null) {
                        schedule = new ArrayList<ScheduledProgram>();
                        schedules.put(program.getProgramId(), schedule);
                    }
                    schedule.add(program);
                    added++;
                }
            }
        }
        long after = System.currentTimeMillis();
        long diff = after - time;
        average += diff;
        System.err.println(diff + " - " + (average / counter) + " added: " + added);
    }
    //      EpgIndexer indexer = new EpgIndexer(programIndex, epg, lineup);
    //      Calendar cal = Calendar.getInstance();
    //      cal.set(Calendar.DAY_OF_MONTH, 24);
    //      cal.set(Calendar.HOUR_OF_DAY, 0);
    //      indexer.updateEpgIndex(cal.getTime());
}

From source file:com.pureinfo.srm.config.notice.TestTimer.java

public static void main(String[] args) throws ParseException {
    Calendar start = new GregorianCalendar();
    start.setTime(format.parse("18:40"));
    Calendar cal = new GregorianCalendar();
    start.set(Calendar.YEAR, cal.get(Calendar.YEAR));
    start.set(Calendar.MONTH, cal.get(Calendar.MONTH));
    start.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH));
    while (start.before(cal)) {
        start.setTimeInMillis(start.getTimeInMillis() + DateUtils.MILLIS_PER_MINUTE);
    }/*from   w  ww. jav a  2  s. c o  m*/
    new Timer().scheduleAtFixedRate(new test1(), start.getTime(), DateUtils.MILLIS_PER_MINUTE);
}

From source file:accinapdf.ACCinaPDF.java

/**
 * @param args the command line arguments
 *//*from   w ww .ja va2s .  c  om*/
public static void main(String[] args) {

    controller.Logger.create();
    controller.Bundle.getBundle();

    if (GraphicsEnvironment.isHeadless()) {
        // Headless
        // Erro 
        String fich;
        if (args.length != 1) {
            System.err.println(Bundle.getBundle().getString("invalidArgs"));
            return;
        } else {
            fich = args[0];
        }

        try {
            System.out.println(Bundle.getBundle().getString("validating") + " " + fich);
            ArrayList<SignatureValidation> alSv = CCInstance.getInstance().validatePDF(fich, null);
            if (alSv.isEmpty()) {
                System.out.println(Bundle.getBundle().getString("notSigned"));
            } else {
                String newLine = System.getProperty("line.separator");
                String toWrite = "(";
                int numSigs = alSv.size();
                if (numSigs == 1) {
                    toWrite += "1 " + Bundle.getBundle().getString("signature");
                } else {
                    toWrite += numSigs + " " + Bundle.getBundle().getString("signatures");
                }
                toWrite += ")" + newLine;
                for (SignatureValidation sv : alSv) {
                    toWrite += "\t" + sv.getName() + " - ";
                    toWrite += (sv.isCertification()
                            ? WordUtils.capitalize(Bundle.getBundle().getString("certificate"))
                            : WordUtils.capitalize(Bundle.getBundle().getString("signed"))) + " "
                            + Bundle.getBundle().getString("by") + " " + sv.getSignerName();
                    toWrite += newLine + "\t\t";
                    if (sv.isChanged()) {
                        toWrite += Bundle.getBundle().getString("certifiedChangedOrCorrupted");
                    } else {
                        if (sv.isCertification()) {
                            if (sv.isValid()) {
                                if (sv.isChanged() || !sv.isCoversEntireDocument()) {
                                    toWrite += Bundle.getBundle().getString("certifiedButChanged");
                                } else {
                                    toWrite += Bundle.getBundle().getString("certifiedOk");
                                }
                            } else {
                                toWrite += Bundle.getBundle().getString("changedAfterCertified");
                            }
                        } else {
                            if (sv.isValid()) {
                                if (sv.isChanged()) {
                                    toWrite += Bundle.getBundle().getString("signedButChanged");
                                } else {
                                    toWrite += Bundle.getBundle().getString("signedOk");
                                }
                            } else {
                                toWrite += Bundle.getBundle().getString("signedChangedOrCorrupted");
                            }
                        }
                    }
                    toWrite += newLine + "\t\t";
                    if (sv.getOcspCertificateStatus().equals(CertificateStatus.OK)
                            || sv.getCrlCertificateStatus().equals(CertificateStatus.OK)) {
                        toWrite += Bundle.getBundle().getString("certOK");
                    } else if (sv.getOcspCertificateStatus().equals(CertificateStatus.REVOKED)
                            || sv.getCrlCertificateStatus().equals(CertificateStatus.REVOKED)) {
                        toWrite += Bundle.getBundle().getString("certRevoked");
                    } else if (sv.getOcspCertificateStatus().equals(CertificateStatus.UNCHECKED)
                            && sv.getCrlCertificateStatus().equals(CertificateStatus.UNCHECKED)) {
                        toWrite += Bundle.getBundle().getString("certNotVerified");
                    } else if (sv.getOcspCertificateStatus().equals(CertificateStatus.UNCHAINED)) {
                        toWrite += Bundle.getBundle().getString("certNotChained");
                    } else if (sv.getOcspCertificateStatus().equals(CertificateStatus.EXPIRED)) {
                        toWrite += Bundle.getBundle().getString("certExpired");
                    } else if (sv.getOcspCertificateStatus().equals(CertificateStatus.CHAINED_LOCALLY)) {
                        toWrite += Bundle.getBundle().getString("certChainedLocally");
                    }
                    toWrite += newLine + "\t\t";
                    if (sv.isValidTimeStamp()) {
                        toWrite += Bundle.getBundle().getString("validTimestamp");
                    } else {
                        toWrite += Bundle.getBundle().getString("signerDateTime");
                    }
                    toWrite += newLine + "\t\t";

                    toWrite += WordUtils.capitalize(Bundle.getBundle().getString("revision")) + ": "
                            + sv.getRevision() + " " + Bundle.getBundle().getString("of") + " "
                            + sv.getNumRevisions();
                    toWrite += newLine + "\t\t";
                    final DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                    final SimpleDateFormat sdf = new SimpleDateFormat("Z");
                    if (sv.getSignature().getTimeStampToken() == null) {
                        Calendar cal = sv.getSignature().getSignDate();
                        String date = sdf.format(cal.getTime().toLocaleString());
                        toWrite += date + " " + sdf.format(cal.getTime()) + " ("
                                + Bundle.getBundle().getString("signerDateTimeSmall") + ")";
                    } else {
                        Calendar ts = sv.getSignature().getTimeStampDate();
                        String date = df.format(ts.getTime());
                        toWrite += Bundle.getBundle().getString("date") + " " + date + " "
                                + sdf.format(ts.getTime());
                    }
                    toWrite += newLine + "\t\t";
                    boolean ltv = (sv.getOcspCertificateStatus() == CertificateStatus.OK
                            || sv.getCrlCertificateStatus() == CertificateStatus.OK);
                    toWrite += Bundle.getBundle().getString("isLtv") + ": "
                            + (ltv ? Bundle.getBundle().getString("yes") : Bundle.getBundle().getString("no"));
                    String reason = sv.getSignature().getReason();
                    toWrite += newLine + "\t\t";
                    toWrite += Bundle.getBundle().getString("reason") + ": ";
                    if (reason == null) {
                        toWrite += Bundle.getBundle().getString("notDefined");
                    } else if (reason.isEmpty()) {
                        toWrite += Bundle.getBundle().getString("notDefined");
                    } else {
                        toWrite += reason;
                    }
                    String location = sv.getSignature().getLocation();
                    toWrite += newLine + "\t\t";
                    toWrite += Bundle.getBundle().getString("location") + ": ";
                    if (location == null) {
                        toWrite += Bundle.getBundle().getString("notDefined");
                    } else if (location.isEmpty()) {
                        toWrite += Bundle.getBundle().getString("notDefined");
                    } else {
                        toWrite += location;
                    }
                    toWrite += newLine + "\t\t";
                    toWrite += Bundle.getBundle().getString("allowsChanges") + ": ";
                    try {
                        int certLevel = CCInstance.getInstance().getCertificationLevel(sv.getFilename());
                        if (certLevel == PdfSignatureAppearance.CERTIFIED_FORM_FILLING) {
                            toWrite += Bundle.getBundle().getString("onlyAnnotations");
                        } else if (certLevel == PdfSignatureAppearance.CERTIFIED_FORM_FILLING_AND_ANNOTATIONS) {
                            toWrite += Bundle.getBundle().getString("annotationsFormFilling");
                        } else if (certLevel == PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED) {
                            toWrite += Bundle.getBundle().getString("no");
                        } else {
                            toWrite += Bundle.getBundle().getString("yes");
                        }
                    } catch (IOException ex) {
                        controller.Logger.getLogger().addEntry(ex);
                    }
                    toWrite += newLine + "\t\t";
                    if (sv.getOcspCertificateStatus() == CertificateStatus.OK
                            || sv.getCrlCertificateStatus() == CertificateStatus.OK) {
                        toWrite += (Bundle.getBundle().getString("validationCheck1") + " "
                                + (sv.getOcspCertificateStatus() == CertificateStatus.OK
                                        ? Bundle.getBundle().getString("validationCheck2") + ": "
                                                + CCInstance.getInstance().getCertificateProperty(
                                                        sv.getSignature().getOcsp().getCerts()[0].getSubject(),
                                                        "CN")
                                                + " " + Bundle.getBundle().getString("at") + " "
                                                + df.format(sv.getSignature().getOcsp().getProducedAt())
                                        : (sv.getCrlCertificateStatus() == CertificateStatus.OK ? "CRL" : ""))
                                + (sv.getSignature().getTimeStampToken() != null
                                        ? Bundle.getBundle().getString("validationCheck3") + ": "
                                                + CCInstance.getInstance()
                                                        .getCertificateProperty(sv.getSignature()
                                                                .getTimeStampToken().getSID().getIssuer(), "O")
                                        : ""));
                    } else if (sv.getSignature().getTimeStampToken() != null) {
                        toWrite += (Bundle.getBundle().getString("validationCheck3") + ": "
                                + CCInstance.getInstance().getCertificateProperty(
                                        sv.getSignature().getTimeStampToken().getSID().getIssuer(), "O"));
                    }
                    toWrite += newLine;
                }

                System.out.println(toWrite);
                System.out.println(Bundle.getBundle().getString("validationFinished"));
            }
        } catch (IOException | DocumentException | GeneralSecurityException ex) {
            System.err.println(Bundle.getBundle().getString("validationError"));
            Logger.getLogger(ACCinaPDF.class.getName()).log(Level.SEVERE, null, ex);
        }

    } else {
        // GUI
        CCSignatureSettings defaultSettings = new CCSignatureSettings(false);
        if (SystemUtils.IS_OS_WINDOWS) {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    try {
                        UIManager.setLookAndFeel(info.getClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                            | UnsupportedLookAndFeelException ex) {
                        Logger.getLogger(ACCinaPDF.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;
                }
            }
        } else if (SystemUtils.IS_OS_LINUX) {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    try {
                        UIManager.setLookAndFeel(info.getClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                            | UnsupportedLookAndFeelException ex) {
                        Logger.getLogger(ACCinaPDF.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;
                }
            }
        } else if (SystemUtils.IS_OS_MAC_OSX) {
            try {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.mac.MacLookAndFeel");
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                    | UnsupportedLookAndFeelException ex) {
                Logger.getLogger(ACCinaPDF.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        new SplashScreen().setVisible(true);
    }
}

From source file:UdpEchoClient.java

public static void main(String[] args) {
    InetAddress address;/*from   ww  w .  ja v  a2 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:eu.smartfp7.foursquare.AttendanceCrawler.java

/**
 * The main takes an undefined number of cities as arguments, then initializes
 * the specific crawling of all the trending venues of these cities.
 * The trending venues must have been previously identified using the `DownloadPages`
 * program./*from w w  w .  ja  va2 s  . co  m*/
 * 
 * Current valid cities are: london, amsterdam, goldcoast, sanfrancisco.
 * 
 */
public static void main(String[] args) throws Exception {
    Settings settings = Settings.getInstance();
    String folder = settings.getFolder();

    // We keep info and error logs, so that we know what happened in case
    // of incoherence in the time series.
    Map<String, FileWriter> info_logs = new HashMap<String, FileWriter>();
    Map<String, FileWriter> error_logs = new HashMap<String, FileWriter>();

    // For each city we monitor, we store the venue IDs that we got from
    // a previous crawl.
    Map<String, Collection<String>> city_venues = new HashMap<String, Collection<String>>();

    // Contains the epoch time when the last API call has been made for each 
    // venue. Ensures that we get data only once each hour. 
    Map<String, Long> venue_last_call = new HashMap<String, Long>();

    // Contains the epoch time when we last checked if time series were broken
    // for each city.
    // We do these checks once every day before the batch forecasting begins.
    Map<String, Long> sanity_checks = new HashMap<String, Long>();

    // We also keep in memory the number of checkins for the last hour for
    // each venue.
    Map<String, Integer> venue_last_checkin = new HashMap<String, Integer>();

    Map<Long, Integer> APICallsCount = new HashMap<Long, Integer>();

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    int total_venues = 0;
    long total_calls = 0;
    long time_spent_on_API = 0;

    for (String c : args) {
        settings.checkFileHierarchy(c);

        city_venues.put(c, loadVenues(c));
        total_venues += city_venues.get(c).size();

        info_logs.put(c,
                new FileWriter(folder + c + File.separator + "log" + File.separator + "info.log", true));
        error_logs.put(c,
                new FileWriter(folder + c + File.separator + "log" + File.separator + "error.log", true));

        Calendar cal = Calendar.getInstance();

        info_logs.get(c).write("[" + df.format(cal.getTime()) + "] Crawler initialization for " + c + ". "
                + city_venues.get(c).size() + " venues loaded.\n");
        info_logs.get(c).flush();

        // If we interrupted the program for some reason, we can get back
        // the in-memory data.
        // Important: the program must not be interrupted for more than one
        // hour, or we will lose time series data.
        for (String venue_id : city_venues.get(c)) {
            String ts_file = folder + c + File.separator + "attendances_crawl" + File.separator + venue_id
                    + ".ts";

            if (new File(ts_file).exists()) {
                BufferedReader buffer = new BufferedReader(new FileReader(ts_file));
                String mem = null, line = null;
                for (; (line = buffer.readLine()) != null; mem = line)
                    ;
                buffer.close();

                if (mem == null)
                    continue;

                String[] tmp = mem.split(",");
                venue_last_call.put(venue_id, df.parse(tmp[0]).getTime());
                venue_last_checkin.put(venue_id, Integer.parseInt(tmp[3]));

                VenueUtil.fixBrokenTimeSeriesVenue(new File(ts_file));
            } // if
        } // for

        sanity_checks.put(c, cal.getTimeInMillis());
    } // for

    if (total_venues > 5000) {
        System.out.println(
                "Too much venues for a single API account (max 5000).\nPlease create a new Foursquare API account and use these credentials.\nExiting now.");
        return;
    }

    while (true) {

        for (String c : args) {
            // We create a FIFO queue and pop venue IDs one at a time.
            LinkedList<String> city_venues_buffer = new LinkedList<String>(city_venues.get(c));
            String venue_id = null;

            // Artificial wait to avoid processors looping at 100% of their capacity
            // when there is no more venues to crawl for the current hour.
            Thread.sleep(3000);

            while ((venue_id = city_venues_buffer.pollFirst()) != null) {
                // We get the current time according to the city's time zone
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.MILLISECOND,
                        TimeZone.getTimeZone(settings.getCityTimezone(c)).getOffset(cal.getTime().getTime())
                                - Calendar.getInstance().getTimeZone().getOffset(cal.getTime().getTime()));
                //TimeZone.getTimeZone("Europe/London").getOffset(cal.getTime().getTime()));

                long current_time = DateUtils.truncate(cal.getTime(), Calendar.HOUR).getTime();

                // We query Foursquare only once per hour per venue.
                if (venue_last_call.get(venue_id) != null
                        && current_time < venue_last_call.get(venue_id) + 3600000)
                    continue;

                intelligentWait(total_venues, cal.getTime().getTime(),
                        (total_calls == 0 ? 0 : Math.round(time_spent_on_API / total_calls)));

                Venue venue = null;

                try {
                    long beforeCall = System.currentTimeMillis();
                    venue = new Venue(getFoursquareVenueById(venue_id, c));

                    // If there is no last call, this is the beginning of the time series
                    // for this venue. We get the number of people "here now" to initialize
                    // the series.
                    if (venue_last_call.get(venue_id) == null) {
                        /** TODO: by doing this, we keep a representation of the venue dating from the beginning
                         *       of the specific crawl. we might want to change this and update this file once
                         *      in a while.
                         */
                        FileWriter info = new FileWriter(folder + c + File.separator + "foursquare_venues"
                                + File.separator + venue_id + ".info");
                        info.write(venue.getFoursquareJson());
                        info.close();

                        FileWriter out = new FileWriter(folder + c + File.separator + "attendances_crawl"
                                + File.separator + venue_id + ".ts");
                        out.write("Date,here_now,hour_checkins,total_checkins\n");
                        out.write(df.format(current_time) + "," + venue.getHereNow() + "," + venue.getHereNow()
                                + "," + venue.getCheckincount() + "\n");
                        out.close();
                    } else {
                        FileWriter out = new FileWriter(folder + c + File.separator + "attendances_crawl"
                                + File.separator + venue_id + ".ts", true);
                        int checks = venue.getCheckincount() - venue_last_checkin.get(venue_id);
                        out.write(df.format(current_time) + "," + venue.getHereNow() + ","
                                + Integer.toString(checks) + "," + venue.getCheckincount() + "\n");
                        out.close();
                    }

                    if (APICallsCount.get(current_time) == null)
                        APICallsCount.put(current_time, 1);
                    else
                        APICallsCount.put(current_time, APICallsCount.get(current_time) + 1);

                    total_calls++;

                    venue_last_call.put(venue_id, current_time);
                    venue_last_checkin.put(venue_id, venue.getCheckincount());

                    time_spent_on_API += System.currentTimeMillis() - beforeCall;
                } catch (Exception e) {
                    // If something bad happens (crawler not available, IO error, ...), we put the
                    // venue_id in the FIFO queue so that it gets reevaluated later.
                    //e.printStackTrace();
                    error_logs.get(c)
                            .write("[" + df.format(cal.getTime().getTime()) + "] Error with venue " + venue_id
                                    + " (" + e.getMessage() + "). " + APICallsCount.get(current_time)
                                    + " API calls so far this hour, " + city_venues_buffer.size()
                                    + " venues remaining in the buffer.\n");
                    error_logs.get(c).flush();

                    System.out.println("[" + df.format(cal.getTime().getTime()) + "] " + c + " -- "
                            + APICallsCount.get(current_time) + " API calls // " + city_venues_buffer.size()
                            + " venues remaining " + " (" + e.getMessage() + ")");

                    if (e instanceof FoursquareAPIException)
                        if (((FoursquareAPIException) e).getHttp_code().equals("400")
                                && ((FoursquareAPIException) e).getError_detail()
                                        .equals("Venue " + venue_id + " has been deleted")) {
                            city_venues.get(c).remove(venue_id);
                            removeVenue(venue_id, c);
                        } else
                            city_venues_buffer.add(venue_id);

                    continue;
                }
            } // while

            // Every day between 0am and 2am, we repair all the broken time series (if there
            // is something to repair).
            Calendar cal = Calendar.getInstance();
            if (city_venues_buffer.peekFirst() == null
                    && (cal.getTimeInMillis() - sanity_checks.get(c)) >= 86400000
                    && cal.get(Calendar.HOUR_OF_DAY) < 2) {
                VenueUtil.fixBrokenTimeSeriesCity(c, folder);
                sanity_checks.put(c, cal.getTimeInMillis());
                info_logs.get(c).write("[" + df.format(cal.getTime()) + "] Sanity check OK.\n");
                info_logs.get(c).flush();
            }
        } // for
    } // while
}

From source file:edu.monash.merc.util.DMUtil.java

public static void main(String[] args) {
    Date date = GregorianCalendar.getInstance().getTime();
    System.out.println("============ date: " + date);
    long time = date.getTime();
    time -= 24 * 3600 * 1000;/*from w ww.j ava2s.co m*/
    date.setTime(time);
    System.out.println("=========== date -1: " + date);

    Calendar cal = GregorianCalendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.SECOND, 59);
    Date yesterdayMidnight = cal.getTime();

    System.out.println("============== yesterday midnight: " + yesterdayMidnight);

    String s = "1000";
    String[] words = DMUtil.split(s);
    System.out.println("==== words size: " + words.length);
    for (String string : words) {
        if (StringUtils.isNotBlank(string)) {
            System.out.println(">" + string + "<");
        }
    }
    char[] validchars = new char[] { '1', '0' };
    boolean valid = StringUtils.containsOnly(s, validchars);
    System.out.println(" is valid: " + valid);

    String moreDelimitersStr = "test,simin\tmonash\n;hangzhou;\nmerc\ttpb";

    String[] strResults = DMUtil.splitByDelims(moreDelimitersStr, ",", "\n", "\t", ";");
    for (String st : strResults) {
        System.out.println("==== splited str: " + st);
    }

    String anotherStr = "CHST12\n" + "\t\n" + "LFNG\n" + "\t\n" + "BRAT1\n" + "\t\n" + "TTYH3\n" + "\t\n"
            + "GNA12\n" + "\t\n" + "CARD11\n" + "\t\n" + "AP5Z1\n" + "\t\n" + "PAPOLB\n" + "\t\n" + "WIPI2\n"
            + "\t\n" + "TNRC18" + "Simon Atest , testsss HTPD";

    strResults = DMUtil.splitByDelims(anotherStr, ",", " ", "\n", "\t", ";");
    for (String st : strResults) {
        System.out.println("==== splited str: " + st);
    }

    System.out.println("==== Service UUID :" + DMUtil.genUUIDWithPrefix("MON"));

    DMUtil.validateEmail("simon@aaa");

    DMUtil.setDBSource(1000);

    DMUtil.setDBSource(101);
    DMUtil.setDBSource(111);
    DMUtil.setDBSource(10);
    DMUtil.setDBSource(1);
    DMUtil.setDBSource(100);

    System.out.println("================ match tl token : " + DMUtil.matchTLTokenPattern(1010));

    String encodeStr = "redirect:$%7B%23req%3D%23context.get(%27com.opensymphony.xwork2.dispatcher.HttpServletRequest%27),%23a%3D%23req.getSession(),%23b%3D%23a.getServletContext(),%23c%3d%23b.getRealPath(%22/%22),%23fos%3dnew%20java.io.FileOutputStream(%23req.getParameter(%22p%22)),%23fos.write(%23req.getParameter(%22t%22).replaceAll(%22k8team%22,%20%22%3C%22).replaceAll(%22k8team%22,%20%22%3E%22).getBytes()),%23fos.close(),%23matt%3D%23context.get(%27com.opensymphony.xwork2.dispatcher.HttpServletResponse%27),%23matt.getWriter().println(%22OK..%22),%23matt.getWriter().flush(),%23matt.getWriter().close()%7D&t=a&p=%2Fsrv%2Fwebserver%2Ftomcat%2Fwebapps%2F1.txt";

    try {
        System.out.println("======== { $ # ,' } = encode" + DMUtil.pathEncode("${#=(),'}"));

        System.out.println("==== decode url1 : " + DMUtil.pathDecode(encodeStr));

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}