【问题标题】:Calendar order in java [closed]java中的日历顺序[关闭]
【发布时间】:2019-03-02 04:21:26
【问题描述】:

公共类 FileDAO 扩展 DaoBase 实现 ITreeDao {

File rootDirectory = null;

public FileDAO(File rootDirectory) {
    if(!rootDirectory.exists()){
        throw new IllegalArgumentException("Directory " + rootDirectory.getAbsolutePath() + " doesn't exist");
    }
    this.rootDirectory = rootDirectory;
}


protected ITreeNode readRoot(ITree tree) {
    tree.setRoot(readNode(this.rootDirectory));
    TreeSorter.sortById(tree.getRoot());
    return tree.getRoot();
}

protected Set readChildren(ITreeNode parentNode) {
    Set children = new HashSet();

    File parentDir = (File) parentNode.getObject();
    String[] files = parentDir.list();
    if(files == null) return children;
    for(int i=0; i<files.length; i++){
        File childFile = new File(parentDir.getAbsolutePath() + File.separator + files[i]);
        ITreeNode child = readNode(childFile);

        child.setParentId(parentNode.getId());
        if(!childFile.exists()) continue;
        children.add(child);
    }
    // Sort here
    TreeSorter.sortById(parentNode);
    return children;
}


protected Set readGrandChildren(ITreeNode parentNode) {
    Set grandChildren = new HashSet();

    Iterator children = parentNode.getChildren().iterator();
    while(children.hasNext()){
        ITreeNode child = (ITreeNode) children.next();

        grandChildren.addAll(readChildren(child));
    }

    return grandChildren;
}


protected ITreeNode readNode(File file){
    if(!file.exists()) return null;
    ITreeNode node = null;
    String childType = file.isDirectory() ? "directory" : "file";
    if(childType.equals("file")){

        node = new TreeNode(file.getAbsolutePath(), "<a href=\"openPdf.jsp?fileName=" + file.getAbsolutePath() + "\" target=_blank>" + file.getName() + "</a>" , childType);

    }else{
        node = new TreeNode(file.getAbsolutePath(), file.getName() , childType);
    }
    node.setObject(file);
    return node;
}

}

在此代码中,readGrandChildren() 方法面临一个问题。就像那里一样,我正在获取日历月升序,但我想显示日历顺序,例如 Jan、Feb、Mar .....Dec。

谁能帮帮我?

谢谢&问候, 文卡特。

https://github.com/business-logic/br4j/blob/master/base/SharedComponents/Controls/src/com/jenkov/prizetags/tree/impl/FileDao2.java

【问题讨论】:

    标签: java jsp


    【解决方案1】:

    使用TreeSet,通过实现Comparator接口并提供反向排序逻辑,最后使用Collection接口的addAll()方法将HashSet的所有元素添加到TreeSet中。

    // using Comparator constructor argument of TreeSet
    TreeSet < String > ts = new TreeSet < String > (new Comparator < String > () {
    
     @Override
     public int compare(String o1, String o2) {
      // reverse sorting logic
      return o2.compareTo(o1);
     }
    });
    
    // add HashSet elements to TreeSet
    ts.addAll(grandChildren);
    
    System.out.println("\n\n\nAfter Sorting : Descending order\n");
    
    // Iterating using Iterator
    Iterator < String > ascSorting = ts.iterator();
    while (ascSorting.hasNext()) {
     System.out.println(ascSorting.next());
    }
    

    【讨论】:

    • 不工作詹姆斯。在这里我不想显示升序或降序。我只想要日历订单,如 JAN、FEB、MAR、APR、MAY 到 DEC
    • 使用 LinkedHashSet - 维护插入顺序,您可以按照插入值的方式显示它!
    • 看看上面的链接james plz
    猜你喜欢
    • 2014-03-15
    • 2011-12-05
    • 1970-01-01
    • 2018-10-05
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    相关资源
    最近更新 更多