在SimpleDateFormat中将Date转换为String

时间:2017-01-20 07:45:57

标签: java date simpledateformat

我想将日期格式转换为字符串,但字符串输出不会出现

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TextInput,
  TouchableOpacity,
  Navigator,
  TouchableHighlight
} from 'react-native';

export default class calu extends Component {
  constructor(props){
    super(props);
    this.state={f2:null, height:null,result:0};
    this.compute=this.compute.bind(this);
  }
  compute(){
    console.log('pressed');
    let f1=(this.state.f1);
    let f2=parseInt(this.state.f2);

    this.setState({result: f1 + f2 });
  }

  render() {

    return (
 <View>

 <TextInput 
 keyboardType='numeric' onChangeText={(text) => this.setState({f1: parseInt(text)})}  placeholder='enter second number' />


<TextInput 
 keyboardType='numeric' onChangeText={(text) => this.setState({f2: parseInt(text)})}  placeholder='enter second number' />




 <Text> result: {this.state.result.toFixed(2)} </Text>
 <TouchableOpacity 
 onPress={this.compute}>
 <Text> compute </Text>
 </TouchableOpacity>  
 </View>
    );
  }
}



AppRegistry.registerComponent('calu', () => calu);

1 个答案:

答案 0 :(得分:8)

要格式化当前日期,您需要创建一个代表当前日期的Date实例,然后使用SimpleDateFormat将其格式化为字符串。

DateFormat dateFormat = new SimpleDateFormat("dd MM yyyy");
String currDateString = dateFormat.format(new Date());
System.out.println(currDateString);