zoukankan      html  css  js  c++  java
  • 利用字符串(StringBuffer)截取格式化日期

    一、在日常中会大量使用日期类,取出日期的操作

    形式一:2012-11-07

    形式二:2012-11-0707:08:26.236

    形式三:2012年11月07日

    形式四:2012年11月07日 07时08分26秒236毫秒

    package edu.datetime; 

    import java.util.Calendar;

    import java.util.GregorianCalendar;

    class DateTime{               //2012-03-06类型

           private Calendar calendar =new GregorianCalendar();    //实例化calendar对象

                  public  String getDate(){

                   StringBuffer buf=new StringBuffer();

                   buf.append(calendar.get(Calendar.YEAR)).append("-");

                   buf.append(this.insertZero(calendar.get(Calendar.MONTH),2)).append("-");

                   buf.append(this.insertZero(calendar.get(Calendar.DATE),2)).append(" ");             

                   buf.append(this.insertZero(calendar.get(Calendar.HOUR_OF_DAY),2)).append(":");

                   buf.append(this.insertZero(calendar.get(Calendar.MINUTE),2)).append(":");

                   buf.append(this.insertZero(calendar.get(Calendar.SECOND),2)).append(":");

                   buf.append(this.insertZero(calendar.get(Calendar.MILLISECOND),3));

                   return buf.toString();

                  }

    private String insertZero(int temp,int len){

           StringBuffer str=new StringBuffer();

           str.append(temp);//加入数字

           while(str.length()<len){

                  str.insert(0,0); //在第一个位置上加上0

              }

           return str.toString();

    public static void main(String args[]){

                  System.out.println(new DateTime().getDate());   }

    }

    }

  • 相关阅读:
    centos rm -rf 恢复删除的文件
    默认hosts后面为files dns
    linux shell expr 使用
    QQ,MSN,Skype在线客服代码
    LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android
    php读取和保存base64编码的图片内容
    linux shell 字符串操作(长度,查找,替换)详解
    tomcat的简单配置与适用默认的web应用
    mybatis左连接需要输出左表的指定内容与筛选
    first head in html 笔记
  • 原文地址:https://www.cnblogs.com/jiechn/p/2768920.html
Copyright © 2011-2022 走看看