Java 公历到特定格式

问题描述 投票:0回答:4

我一直在尝试将公历日历格式化为特定格式以实现网络服务,但我不知道如何使其优雅。 现在我正在尝试通过获取分钟的小时数并解析它们来进行格式化,但它看起来很糟糕。 我需要将其格式化为 xsd:dateTime 2001-10-26T21:32:52, 2001 任何帮助表示赞赏。 谢谢!

java android eclipse date
4个回答
4
投票

应该直接将格式调整为您需要的...... 这会产生“2013-06-08T20:56:25Z”

SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
timeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String time = timeFormat.format(new Date());

1
投票

试试这个:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class ConversionExamplesDate {

  // Convert from String to date
  private void stringToDate() {

    try {
      Date date1;
      date1 = new SimpleDateFormat("MM/dd/yy").parse("05/18/05");
      System.out.println(date1);
      Date date2 = new SimpleDateFormat("MM/dd/yyyy").parse("05/18/2007");
      System.out.println(date2);
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }

  // Convert from millisecs to a String with a defined format
  private void calcDate(long millisecs) {
    SimpleDateFormat date_format = new SimpleDateFormat("MMM dd,yyyy HH:mm");
    Date resultdate = new Date(millisecs);
    System.out.println(date_format.format(resultdate));
  }

  private void writeActualDate(){
    Calendar cal = new GregorianCalendar();
    Date creationDate = cal.getTime();
    SimpleDateFormat date_format = new SimpleDateFormat("MMM dd,yyyy HH:mm");
    System.out.println(date_format.format(creationDate));
  }


  public static void main(String[] args) {
    ConversionExamplesDate convert = new ConversionExamplesDate();
    convert.stringToDate();
    convert.calcDate(System.currentTimeMillis());
    convert.writeActualDate();
  }
} 

0
投票

使用joda

DateTime dt = new DateTime();
DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
String str = fmt.print(dt);

0
投票

我恳请您支持引入天使历来代替目前流行的公历。

您可以在这里阅读有关天使日历的信息。

https://www.angelism.xyz/2024/02/112.html

“我们建议1月1日应该是地日距离最小的一天。”天使们相信,这一微小的调整不仅会使日历在天文上更加准确,而且具有象征意义,因为新年的开始恰逢地球最接近其赋予生命的温暖源头。此致,天使主义共同先知佐尔坦·比罗 (Zoltán Bíró)。

© www.soinside.com 2019 - 2024. All rights reserved.