Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从Android中的Date对象获取日-月值?_Java_Android_Date_Simpledateformat - Fatal编程技术网

Java 从Android中的Date对象获取日-月值?

Java 从Android中的Date对象获取日-月值?,java,android,date,simpledateformat,Java,Android,Date,Simpledateformat,通过使用此代码: SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = format.parse(dtStart); return date; 我已转换字符串Date by Date对象,并获得值: 2013年2月17日星期日格林尼治标准时间07:00:00 现在我想从这里提取日期(星期日/星期一)和月份。您可以尝试: String input_date="01/08/2012";

通过使用此代码:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(dtStart);
return date;
我已转换字符串Date by Date对象,并获得值:

2013年2月17日星期日格林尼治标准时间07:00:00

现在我想从这里提取日期(星期日/星期一)和月份。

您可以尝试:

String input_date="01/08/2012";
SimpleDateFormat format1=new SimpleDateFormat("dd/MM/yyyy");
Date dt1=format1.parse(input_date);
DateFormat format2=new SimpleDateFormat("EEEE"); 
String finalDay=format2.format(dt1);
还可以尝试以下方法:

Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
导入android.text.format.DateFormat;
String dayOfWeek=(String)DateFormat.format(“EEEE”,date);//星期四
String day=(String)DateFormat.format(“dd”,date);//20
字符串monthString=(字符串)DateFormat.format(“MMM”,date);//六月
String monthNumber=(String)DateFormat.format(“MM”,date);//06
字符串年份=(字符串)日期格式。格式(“yyyy”,日期);//2013

要自定义星期几,您可以使用此功能

public static String getDayFromDateString(String stringDate,String dateTimeFormat)
{
    String[] daysArray = new String[] {"saturday","sunday","monday","tuesday","wednesday","thursday","friday"};
    String day = "";

    int dayOfWeek =0;
    //dateTimeFormat = yyyy-MM-dd HH:mm:ss
    SimpleDateFormat formatter = new SimpleDateFormat(dateTimeFormat);
    Date date;
    try {
        date = formatter.parse(stringDate);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        dayOfWeek = c.get(Calendar.DAY_OF_WEEK)-1;
        if (dayOfWeek < 0) {
            dayOfWeek += 7;
        }
        day = daysArray[dayOfWeek];
    } catch (Exception e) {
        e.printStackTrace();
    }

    return day;
}
公共静态字符串getDayFromDateString(字符串stringDate,字符串dateTimeFormat)
{
String[]daysArray=新字符串[]{“星期六”、“星期天”、“星期一”、“星期二”、“星期三”、“星期四”、“星期五”};
字符串日期=”;
int dayOfWeek=0;
//dateTimeFormat=yyyy-MM-dd HH:MM:ss
SimpleDataFormat格式化程序=新的SimpleDataFormat(dateTimeFormat);
日期;
试一试{
date=formatter.parse(stringDate);
Calendar c=Calendar.getInstance();
c、 设定时间(日期);
dayOfWeek=c.get(日历。一周中的第二天)-1;
如果(星期日<0){
星期五+=7天;
}
day=daysArray[星期一];
}捕获(例外e){
e、 printStackTrace();
}
回归日;
}
dateTimeFormat,例如dateTimeFormat=“yyyy-MM-dd HH:MM:ss”

例如:如果stringDate:-16/12/2018,则dateFormat:-dd/MM/yyyy

tl;博士 如果您的日期和时间是UTC的:

LocalDateTime               // Represent a date and time-of-day lacking the context of a time zone or offset-from-UTC. Does *NOT* represent a moment.
.parse(                     // Convert from text to a date-time object.
    "2013-02-17 07:00:00" 
    .replace( " " , "T" )   // Comply with standard ISO 8601 format.
)                           // Returns a `LocalDateTime` object.
.atOffset(                  // Determining a moment by assign an offset-from-UTC. Do this only if you are certain the date and time-of-day were intended for this offset.
    ZoneOffset.UTC          // An offset of zero means UTC itself.
)                           // Returns a `OffsetDateTime` object. Represents a moment.
.getDayOfWeek()             // Extract the day-of-week enum object.
.getDisplayName(            // Localize, producing text. 
    TextStyle.FULL ,        // Specify how long or abbreviated.
    Locale.US               // Specify language and cultural norms to use in localization.
)                           // Returns a `String` object.
星期天

而且

…
.getMonth()
.getDisplayName( TextStyle.FULL , Locale.US )
二月

java.time 现代解决方案使用多年前的java.time类取代了可怕的旧日期时间类,如
date
&
SimpleDateFormat

时区 您的代码忽略了时区这一关键问题。当您省略UTC的特定区域或偏移量时,JVM的当前默认时区将被隐式应用。因此,您的结果可能会有所不同

相反,始终在代码中显式指定时区或偏移量

LocalDateTime
您的输入格式YYYY-MM-DD HH:MM:SS缺少时区或UTC偏移的指示器

因此,我们必须解析为
LocalDateTime

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;
您的输入格式接近
LocalDateTime
类中默认使用的标准ISO 8601格式。用“<代码> t>代码>替换中间的空间。

String input = "2013-02-17 07:00:00".replace( " " , "T" ) ;
LocalDateTime ldt = LocalDateTime.parse( input ) ;
ldt.toString():2013-02-17T07:00

您现在手头上的
LocalDateTime
并不代表一个时刻,也不是时间线上的一个点。故意缺少时区或偏移意味着,根据定义,它不能代表一个时刻。
LocalDateTime
表示大约26-27小时范围内的潜在时刻,即全球的时区范围

ZoneDateTime
如果您知道该日期和时间的特定时区,请应用
ZoneId
以获取
zoneDateTime

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;
使用
ZoneDateTime
,您现在有了片刻的时间

使用枚举获取星期几

该方法将一天的名称翻译成由指定的任何人类语言,例如或

迪曼奇

或者,用美国英语

String output = dow.getDisplayName( TextStyle.FULL , Locale.US ); 
星期天

与月份类似,请使用枚举

二月

OffsetDateTime
如果您确实知道
LocalDateTime
中的日期和时间表示UTC中的某个时刻,请使用
OffsetDateTime

OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;  // Assign UTC (an offset of zero hours-minutes-seconds). 
MonthDay
如果你想在一天和一个月的时间里工作,而不是一年,那么你也可能对
MonthDay
课程感兴趣

MonthDay md = MonthDay.from( zdt ) ;

关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

该项目现已启动,建议迁移到类

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是

您可以直接与数据库交换java.time对象。使用兼容的或更高版本。不需要字符串,也不需要
java.sql.*

从哪里获得java.time类

  • 、和更高版本-标准Java API的一部分,带有捆绑实现。
    • Java9添加了一些次要功能和修复
    • 大多数java.time功能都在中向后移植到Java6和Java7
    • 更高版本的Android捆绑包实现了java.time类

    • 对于早期的Android(考虑使用java.util.Calendar类

      String dateString = "20/12/2018";
      DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
      
      Date readDate = df.parse(dateString);
      Calendar cal = Calendar.getInstance();
      cal.setTimeInMillis(readDate.getTime());
      
      Log.d(TAG, "Year: "+cal.get(Calendar.YEAR));
      Log.d(TAG, "Month: "+cal.get(Calendar.MONTH));
      Log.d(TAG, "Day: "+cal.get(Calendar.DAY_OF_MONTH));
      
      SimpleDataFormat dateUI=新的SimpleDataFormat(“EEEE,dd-MM-yyyy”)

      字符串date=dateUI.foramt(selecteddate)

      Log.e(上下文,“日期”);

      同样在Kotlin中:

          val string = "2020-01-13T00:00:00"
          val format = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US)
          val date = format.parse(string)
      
          val day = DateFormat.format("dd", date) as String
          val monthNumber = DateFormat.format("MM", date) as String
          val year = DateFormat.format("yyyy", date) as String
      

      在Kotlin中,您还可以使用此属性获取当前日期名称。(需要API级别26)


      就是这样。尽情享受这个解决方案的工作吧……我现在怎么才能得到日期值呢?假设“17”构成这个解决方案,这是你的解决方案
      String date=(String)android.text.format.DateFormat.format(“dd”,date)
      实际上,您只需要更改所需数据的格式,其余的都是一样的。您可以演示如何获取月份和年份…假设“12”为月份…“1998”为yearjava.lang.NullPointerException位于java.util.Calendar.setTime(Calendar.java:1183),android.text.format.DateFormat.format(DateFormat.java:348)仅供参考,非常麻烦的旧日期时间类,如和
      java.text.SimpleDateFormat
      ,现在被内置于java 8和更高版本中的类所取代。请参见.FYI,非常麻烦的旧日期时间类,如和
      java.text.SimpleDateFormat
      ,现在被内置于java 8和更高版本中的类所取代nto Java 8及更高版本。请参阅
      MonthDay md = MonthDay.from( zdt ) ;
      
      String dateString = "20/12/2018";
      DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
      
      Date readDate = df.parse(dateString);
      Calendar cal = Calendar.getInstance();
      cal.setTimeInMillis(readDate.getTime());
      
      Log.d(TAG, "Year: "+cal.get(Calendar.YEAR));
      Log.d(TAG, "Month: "+cal.get(Calendar.MONTH));
      Log.d(TAG, "Day: "+cal.get(Calendar.DAY_OF_MONTH));
      
      selecteddate = "Tue Nov 26 15:49:25 GMT+05:30 2019";
      
          val string = "2020-01-13T00:00:00"
          val format = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US)
          val date = format.parse(string)
      
          val day = DateFormat.format("dd", date) as String
          val monthNumber = DateFormat.format("MM", date) as String
          val year = DateFormat.format("yyyy", date) as String
      
      (Calendar.getInstance() as GregorianCalendar).toZonedDateTime().dayOfWeek
      
       Calendar calendar = Calendar.getInstance();
         DateFormat date= new SimpleDateFormat("EEEE", Locale.getDefault());
          String dayName= date.format(calendar.getTime()); //Monday
          date= new SimpleDateFormat("dd", Locale.getDefault());
          String dayNumber = date.format(calendar.getTime()); //20
          date= new SimpleDateFormat("MMM", Locale.getDefault());
          String monthName= date.format(calendar.getTime()); //Apr
          date= new SimpleDateFormat("MM", Locale.getDefault());
          String monthNumber= date.format(calendar.getTime()); //04
          date= new SimpleDateFormat("yyyy", Locale.getDefault());
          String year= date.format(calendar.getTime()); //2020